“PyTest中Python数据生成函数的问题”

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

Issues with python data generator functions in PyTest

问题

It seems like you're experiencing an issue with the child object being shared between instances of the parent. This might be due to the default argument child = [] in your gen_data function, which causes the same list object to be shared among different parent instances. To avoid this, you can use None as the default argument and create a new list if child is None. Here's the corrected code:

# hf.py

# helper functions

def gen_data(data_a, data_b="one", child=None):
    if child is None:
        child = []
    return {
        "data_a": data_a,
        "data_b": data_b,
        "child": child
    }

def gen_child_data(data_1, data_2="two", data_3="three"):
    return {
        "data_1": data_1,
        "data_2": data_2,
        "data_3": data_3
    }

With this change, the child list will be unique to each parent instance, and you should get the expected behavior in your tests.

英文:

Not sure if this is a PyTest issue or Python issue or something that I'm doing wrong. I'm testing my application and have created a couple of data generator functions to speed up creation of test data. One is a parent and one a child. The Parent includes a list of child items.

If I call the parent creation method once and then append a child to it all is good. If I then call the parent method again, for the next test, the child object is then attached to this new instance. I've checked and the IDs of the parents are different so I'm a bit confused.

MRE

hf.py

#helper functions

def gen_data(data_a, data_b = "one", child = []):
    return {
        "data_a": data_a,
        "data_b": data_b,
        "child": child
    }

def gen_child_data(data_1, data_2 = "two", data_3 = "three"):
    return {
        "data_1": data_1,
        "data_2": data_2,
        "data_3": data_3
    }

test.py

from hf import *

def test_One():
    parent = gen_data("parent")
    child = gen_child_data("child")
    parent['child'].append(child)

    print(id(parent), parent)

def test_Two():
    parent = gen_data("parent two")
    print(id(parent), parent)

Running pytest -s -v test.py hits me with the following:

test.py::test_One 4322510784 {'data_a': 'parent', 'data_b': 'one', 'child': [{'data_1': 'child', 'data_2': 'two', 'data_3': 'three'}]}
PASSED
test.py::test_Two 4322601792 {'data_a': 'parent two', 'data_b': 'one', 'child': [{'data_1': 'child', 'data_2': 'two', 'data_3': 'three'}]}
PASSED

My expectation was that test_Two would be {'data_a': 'parent two', 'data_b': 'one', 'child': []}

Am I doing something incorrectly here?

答案1

得分: 1

You are setting the child default value in your gen_data() function to a mutable list, which means that when you append values to it in test_One, you are modifying the child default value.

gen_data()函数中的child默认值设置为可变的list,这意味着当您在test_One中附加值时,您正在修改child的默认值。将默认值设置为None,并在dict中使用简单的if elsechild的值设置为:

def gen_data(data_a, data_b="one", child=None):
    return {
        "data_a": data_a,
        "data_b": data_b,
        "child": child if child else []
    }

def test_Two():
    parent = gen_data("parent two")
    print(id(parent), parent) # 2301583146880 {'data_a': 'parent two', 'data_b': 'one', 'child': []}
英文:

You are setting the child default value in your gen_data() function to a mutable list, that means that when you are appending it values in test_One you are modifying the child default value.

Set the default value to None and set the child value in the dict with a simple if else

def gen_data(data_a, data_b="one", child=None):
    return {
        "data_a": data_a,
        "data_b": data_b,
        "child": child if child else []
    }

def test_Two():
    parent = gen_data("parent two")
    print(id(parent), parent) # 2301583146880 {'data_a': 'parent two', 'data_b': 'one', 'child': []}

huangapple
  • 本文由 发表于 2023年5月14日 20:48:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247576.html
匿名

发表评论

匿名网友

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

确定