Pytest断言错误问题

时间:2015-12-16 00:28:38

标签: python pytest

嗨所以我在这里有一个功能:

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的文本编辑器不太满意

1 个答案:

答案 0 :(得分:1)

cross_product函数修改其参数(例如,t1[0].append(category))。因此,在第一次测试完成后,您的测试列表R1和R2未达到其原始值,已被该功能修改。相反,传递测试列表的副本。