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

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函数对其进行测试时,如果该函数中有多个assert语句,我会不断收到断言错误。我检查了代码,根本不应该有任何断言错误。。。但是我似乎无法修复它。有人能告诉我该如何解决吗?我仍处于学习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并未达到原始值,已通过该功能进行了修改。而是通过测试列表的副本。

10-06 11:14