英文:
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 else将child的值设置为:
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': []}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论