Python py.test函数的排序会导致失败

Python py.test函数的排序会导致失败,python,unit-testing,pytest,Python,Unit Testing,Pytest,我正在教一门使用Python编程的入门课程。我用py.test给学生的作业评分。我遇到这样一种情况,py.test在测试函数处于一个顺序时报告失败的测试用例,但在测试函数处于另一个顺序时报告通过 我用失败的程序和测试代码创建了一个。但这里有一段测试程序的摘录 from exercise3 import union, intersection, difference ########### # TABLES ## ########### GRADUATES = [["Number", "Surn

我正在教一门使用Python编程的入门课程。我用py.test给学生的作业评分。我遇到这样一种情况,py.test在测试函数处于一个顺序时报告失败的测试用例,但在测试函数处于另一个顺序时报告通过

我用失败的程序和测试代码创建了一个。但这里有一段测试程序的摘录

from exercise3 import union, intersection, difference

###########
# TABLES ##
###########
GRADUATES = [["Number", "Surname", "Age"],
             [7274, "Robinson", 37],
             [7432, "O'Malley", 39],
             [9824, "Darkes", 38]]

MANAGERS = [["Number", "Surname", "Age"],
            [9297, "O'Malley", 56],
            [7432, "O'Malley", 39],
            [9824, "Darkes", 38]]


#####################
# HELPER FUNCTIONS ##
#####################
def is_equal(t1, t2):
    return sorted(t1) == sorted(t2)


###################
# TEST FUNCTIONS ##
###################


def test_union():
    """
    Test union operation.
    """

    result = [["Number", "Surname", "Age"],
              [7274, "Robinson", 37],
              [9297, "O'Malley", 56],
              [7432, "O'Malley", 39],
              [9824, "Darkes", 38]]

    assert is_equal(result, union(GRADUATES, MANAGERS))


def test_intersection():
    """
    Test intersection operation.
    """
    result = [["Number", "Surname", "Age"],
              [7432, "O'Malley", 39],
              [9824, "Darkes", 38]]

    assert is_equal(intersection(GRADUATES, MANAGERS), result)
如果我把test_并集函数放在第一位,把test_交集函数放在第二位,后者就会失败。这是py.test的输出。看起来test_intersection中的结果变量正在使用test_union函数中的值

/Users/ses/anaconda/bin/python "/Applications/PyCharm Educational.app/Contents/helpers/pycharm/pytestrunner.py" -p pytest_teamcity /Users/ses/PycharmProjects/pytest_weirdness/test_exercise3.py
Testing started at 1:11 PM ...
============================= test session starts ==============================
platform darwin -- Python 2.7.10 -- py-1.4.27 -- pytest-2.7.1
rootdir: /Users/ses/PycharmProjects/pytest_weirdness, inifile: 
collected 3 items

../../../../Users/ses/PycharmProjects/pytest_weirdness/test_exercise3.py .F
def test_intersection():
        """
        Test intersection operation.
        """
        result = [["Number", "Surname", "Age"],
                  [7432, "O'Malley", 39],
                  [9824, "Darkes", 38]]

    >       assert is_equal(intersection(GRADUATES, MANAGERS), result)
    E       assert is_equal([['Number', 'Surname', 'Age'], [7432, "O'Malley", 39], [9824, 'Darkes', 38], [9297, "O'Malley", 56]], [['Number', 'Surname', 'Age'], [7432, "O'Malley", 39], [9824, 'Darkes', 38]])
    E        +  where [['Number', 'Surname', 'Age'], [7432, "O'Malley", 39], [9824, 'Darkes', 38], [9297, "O'Malley", 56]] = intersection([['Number', 'Surname', 'Age'], [7274, 'Robinson', 37], [7432, "O'Malley", 39], [9824, 'Darkes', 38], [9297, "O'Malley", 56]], [['Number', 'Surname', 'Age'], [9297, "O'Malley", 56], [7432, "O'Malley", 39], [9824, 'Darkes', 38]])
如果我颠倒顺序,两个测试都通过。如果我一次运行一个测试,测试用例就会通过

这让我很困惑,因为我认为测试用例的顺序并不重要。而且,我自己的代码没有问题。作业要到今晚才到期,所以我还没有分享解决方案,但是


我将Anaconda 2.3.0发行版和PyCharm教育版与Python 2.7.10和pytest-2.7.1一起使用。问题是您正在传递一个可更改的
列表,但测试中的函数对它们有副作用

一种可能的解决方案是将列表的深度副本传递给测试函数。使用标准库中的模块

例如


如果它们的
union
intersection
函数实际上更改了
GRADUTES
MANAGERS
的值,那么调用第二个测试用例可能会失败,因为预期的结果不正确。但是Python不是按值调用吗?另外,我已经打印出了表格的内容,它们看起来很好。问题是结果表不正确。我将在帖子中添加错误消息。这取决于您传递的类型。由于
列表
在技术上是可变的,因此您可以使用
list.sort()之类的内容进行就地更改
我花了所有时间盯着测试代码。这个问题可以在test_union中以您建议的方式解决,也可以在union中通过复制输入来解决。
assert is_equal(result, union(copy.deepcopy(GRADUATES), copy.deepcopy(MANAGERS)))