Pytest断言错误问题
Pytest Assertion Error Issue
嗨,我在这里有一个功能:
def cross_product(t1,t2):
new = []
#create new schema
for category in t2[0]:
t1[0].append(category)
new.append(t1[0])
#make table content
if t1 != t2:
for row in t1[1:]:
step = 0
for i in t2[1:]:
new.append(row + i)
if len(new) < 2:
return None
return new
如我所愿。但是,当我使用 py.test 函数对其进行测试时,如果函数中有多个断言语句,我就会不断收到断言错误。我检查了代码,根本不应该有断言错误……但我似乎无法修复它。任何人都可以告诉我我能做些什么来解决这个问题?我仍处于学习的初级阶段 python 所以请尽可能保持简单。
这里是测试用例:
def test_cross_product_basic():
"""
Test cross product operation with basic/generic inputs.
"""
result_1 = [["Employee", "Department", "Department", "Head"],
["Smith", "sales", "production", "Mori"],
["Smith", "sales", "sales", "Brown"],
["Black", "production", "production", "Mori"],
["Black", "production", "sales", "Brown"],
["White", "production", "production", "Mori"],
["White", "production", "sales", "Brown"]]
assert is_equal(result_1, cross_product(R1, R2))
result_2 = [['Department', 'Head', 'Employee', 'Department'],
['production', 'Mori', 'Smith', 'sales'],
['production', 'Mori', 'Black', 'production'],
['production', 'Mori', 'White', 'production'],
['sales', 'Brown', 'Smith', 'sales'],
['sales', 'Brown', 'Black', 'production'],
['sales', 'Brown', 'White', 'production']]
assert is_equal(result_2, cross_product(R2, R1))
result_3 = [['Surname', 'FirstName', 'Age', 'Salary', 'Employee', 'Department'],
['Smith', 'Mary', 25, 2000, 'Smith', 'sales'],
['Smith', 'Mary', 25, 2000, 'Black', 'production'],
['Smith', 'Mary', 25, 2000, 'White', 'production'],
['Black', 'Lucy', 40, 3000, 'Smith', 'sales'],
['Black', 'Lucy', 40, 3000, 'Black', 'production'],
['Black', 'Lucy', 40, 3000, 'White', 'production'],
['Verdi', 'Nico', 36, 4500, 'Smith', 'sales'],
['Verdi', 'Nico', 36, 4500, 'Black', 'production'],
['Verdi', 'Nico', 36, 4500, 'White', 'production'],
['Smith', 'Mark', 40, 3900, 'Smith', 'sales'],
['Smith', 'Mark', 40, 3900, 'Black', 'production'],
['Smith', 'Mark', 40, 3900, 'White', 'production']]
assert is_equal(result_3, cross_product(EMPLOYEES, R1))
这是我正在使用的列表:
R1 = [["Employee", "Department"],
["Smith", "sales"],
["Black", "production"],
["White", "production"]]
EMPLOYEES = [["Surname", "FirstName", "Age", "Salary"],
["Smith", "Mary", 25, 2000],
["Black", "Lucy", 40, 3000],
["Verdi", "Nico", 36, 4500],
["Smith", "Mark", 40, 3900]]
R2 = [["Department", "Head"],
["production", "Mori"],
["sales", "Brown"]]
对于任何格式问题,我深表歉意,我对 SO 的文本编辑器不是很好
cross_product 函数修改其参数(例如,t1[0].append(category)
)。因此,在第一个测试完成后,您的测试列表 R1 和 R2 不再是它们的原始值,而是被函数修改了。而是通过测试列表的副本。
嗨,我在这里有一个功能:
def cross_product(t1,t2):
new = []
#create new schema
for category in t2[0]:
t1[0].append(category)
new.append(t1[0])
#make table content
if t1 != t2:
for row in t1[1:]:
step = 0
for i in t2[1:]:
new.append(row + i)
if len(new) < 2:
return None
return new
如我所愿。但是,当我使用 py.test 函数对其进行测试时,如果函数中有多个断言语句,我就会不断收到断言错误。我检查了代码,根本不应该有断言错误……但我似乎无法修复它。任何人都可以告诉我我能做些什么来解决这个问题?我仍处于学习的初级阶段 python 所以请尽可能保持简单。
这里是测试用例:
def test_cross_product_basic():
"""
Test cross product operation with basic/generic inputs.
"""
result_1 = [["Employee", "Department", "Department", "Head"],
["Smith", "sales", "production", "Mori"],
["Smith", "sales", "sales", "Brown"],
["Black", "production", "production", "Mori"],
["Black", "production", "sales", "Brown"],
["White", "production", "production", "Mori"],
["White", "production", "sales", "Brown"]]
assert is_equal(result_1, cross_product(R1, R2))
result_2 = [['Department', 'Head', 'Employee', 'Department'],
['production', 'Mori', 'Smith', 'sales'],
['production', 'Mori', 'Black', 'production'],
['production', 'Mori', 'White', 'production'],
['sales', 'Brown', 'Smith', 'sales'],
['sales', 'Brown', 'Black', 'production'],
['sales', 'Brown', 'White', 'production']]
assert is_equal(result_2, cross_product(R2, R1))
result_3 = [['Surname', 'FirstName', 'Age', 'Salary', 'Employee', 'Department'],
['Smith', 'Mary', 25, 2000, 'Smith', 'sales'],
['Smith', 'Mary', 25, 2000, 'Black', 'production'],
['Smith', 'Mary', 25, 2000, 'White', 'production'],
['Black', 'Lucy', 40, 3000, 'Smith', 'sales'],
['Black', 'Lucy', 40, 3000, 'Black', 'production'],
['Black', 'Lucy', 40, 3000, 'White', 'production'],
['Verdi', 'Nico', 36, 4500, 'Smith', 'sales'],
['Verdi', 'Nico', 36, 4500, 'Black', 'production'],
['Verdi', 'Nico', 36, 4500, 'White', 'production'],
['Smith', 'Mark', 40, 3900, 'Smith', 'sales'],
['Smith', 'Mark', 40, 3900, 'Black', 'production'],
['Smith', 'Mark', 40, 3900, 'White', 'production']]
assert is_equal(result_3, cross_product(EMPLOYEES, R1))
这是我正在使用的列表:
R1 = [["Employee", "Department"],
["Smith", "sales"],
["Black", "production"],
["White", "production"]]
EMPLOYEES = [["Surname", "FirstName", "Age", "Salary"],
["Smith", "Mary", 25, 2000],
["Black", "Lucy", 40, 3000],
["Verdi", "Nico", 36, 4500],
["Smith", "Mark", 40, 3900]]
R2 = [["Department", "Head"],
["production", "Mori"],
["sales", "Brown"]]
对于任何格式问题,我深表歉意,我对 SO 的文本编辑器不是很好
cross_product 函数修改其参数(例如,t1[0].append(category)
)。因此,在第一个测试完成后,您的测试列表 R1 和 R2 不再是它们的原始值,而是被函数修改了。而是通过测试列表的副本。