英文:
Problem with unsollicited change of value for pytest fixture
问题
I'm here to provide translations for your code. Here's the translated code:
我对pytest还很陌生,无法解决一个令人讨厌的问题。
我想要一个使用dataclass的基本测试值的fixture。对于某些测试函数,我想要在本地修改其中一些值,以测试特定情况。但我希望保持基本值不变,以便在下一个测试函数中使用!
这是我的代码:
```python
import pytest
from dataclasses import dataclass
@dataclass
class Def_Input:
var1 = 3
var2 = 4
@pytest.fixture
def def_inp():
def_inp = Def_Input()
return def_inp
def test_1(def_inp):
temp = def_inp
temp.var1 = 5
assert def_inp.var1 == 3
assert temp.var1 + temp.var2 == 9
第二个断言没有问题,但第一个有问题。我得到一个断言错误,说当我没有修改def_inp时,def_inp.var1 = 5,但我使用了一个临时变量...
您是否有任何解决方法的想法?
编辑:
@Steven97102 这对我提供的示例有效,但我正在处理一个稍微复杂一点的函数。在dataclass中,我从外部类创建对象。类似于这样:
import pytest
import dataclasses
from dataclasses import dataclass
class Tab:
def __init__(self, name):
self.name = name
@dataclass
class Def_Input:
var1 = 3
var2 = 4
name = 'paul'
tab = Tab(name)
@pytest.fixture
def def_inp():
def_inp = Def_Input()
return def_inp
def test_1(def_inp):
temp = dataclasses.replace(def_inp)
temp.var1 = 5
temp.tab.name = 'john'
assert def_inp.tab.name == 'paul'
assert def_inp.var1 == 3
assert temp.var1 + temp.var2 == 9
您是否知道如何使第一个断言生效?
<details>
<summary>英文:</summary>
I'm new to pytest and I can't solve an annoying problem.
I want to have a fixture with base test values from a dataclass. And for certain test functions, I want to to modify some of these values locally to test some specificities. But I want to keep the base values unaltered for the next test functions !
Here's my code:
import pytest
from dataclasses import dataclass
@dataclass
class Def_Input:
var1 = 3
var2 = 4
@pytest.fixture
def def_inp():
def_inp = Def_Input()
return def_inp
def test_1(def_inp):
temp = def_inp
temp.var1 = 5
assert def_inp.var1 == 3
assert temp.var1 + temp.var2 == 9
The 2nd assert is not a problem, but the first one is. I get an AssertionError saying def_inp.var1 = 5 when I didn't modify def_inp, I used a temporary variable...
Would you have any ideas at to solve this?
Edit:
@Steven97102 this does work for the example I gave, but I work on a slightly more complex function. In the dataclass, I create objects from external classes. It's something like this:
import pytest
import dataclasses
from dataclasses import dataclass
class Tab:
def init(self, name):
self.name = name
@dataclass
class Def_Input:
var1 = 3
var2 = 4
name = 'paul'
tab = Tab(name)
@pytest.fixture
def def_inp():
def_inp = Def_Input()
return def_inp
def test_1(def_inp):
temp = dataclasses.replace(def_inp)
temp.var1 = 5
temp.tab.name = 'john'
assert def_inp.tab.name == 'paul'
assert def_inp.var1 == 3
assert temp.var1 + temp.var2 == 9
Would you know I could get the first assert to work?
</details>
# 答案1
**得分**: 0
Python使用引用模型来传递类实例。这意味着`temp = def_inp`仅仅创建一个指向与`def_inp`相同对象的变量`temp`。为了避免修改现有实例,你需要创建一个新的实例。
以下是制作`def_inp`的深度拷贝的解决方案:
```python
import pytest
import dataclasses
from dataclasses import dataclass
@dataclass
class Def_Input:
var1 = 3
var2 = 4
@pytest.fixture
def def_inp():
def_inp = Def_Input()
return def_inp
def test_1(def_inp):
# 制作"def_inp"的深度拷贝
temp = dataclasses.replace(def_inp)
temp.var1 = 5
assert def_inp.var1 == 3
assert temp.var1 + temp.var2 == 9
希望这对你有所帮助。
英文:
Python passes class instances using the reference model. This means that temp = def_inp
simply creates a variable temp
that points to the same object as def_inp
. To avoid modifying the existing instance, you will have to create a new instance of it.
Here's a solution that makes a deep copy of def_inp
:
import pytest
import dataclasses
from dataclasses import dataclass
@dataclass
class Def_Input:
var1 = 3
var2 = 4
@pytest.fixture
def def_inp():
def_inp = Def_Input()
return def_inp
def test_1(def_inp):
# make a deep copy of "def_inp"
temp = dataclasses.replace(def_inp)
temp.var1 = 5
assert def_inp.var1 == 3
assert temp.var1 + temp.var2 == 9
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论