问题出在pytest fixture的值不经意地被更改。

huangapple go评论43阅读模式
英文:

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&#39;m new to pytest and I can&#39;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&#39;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&#39;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&#39;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 &quot;def_inp&quot;
    temp = dataclasses.replace(def_inp)

    temp.var1 = 5
    assert def_inp.var1 == 3
    assert temp.var1 + temp.var2 == 9

huangapple
  • 本文由 发表于 2023年5月11日 17:06:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225902.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定