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

Ordering of py.test functions results in failure

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

我用失败的程序和测试代码创建了一个 project on Github。但这里是测试程序的摘录。

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_union 函数放在第一位,然后将 test_intersection 函数放在第二位,则后者会失败。这是 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]])

如果我颠倒顺序,两个测试都会通过。如果我 运行 一次测试一个,则测试用例通过。

这让我很困惑,因为我认为测试用例的顺序并不重要。另外,我自己的代码没有问题。作业要到今晚才到期,所以我还没有分享解决方案,但是 the original repo is also available on GitHub.

我正在使用 Anaconda 2.3.0 发行版和 PyCharm 教育版以及 Python 2.7.10 和 pytest-2.7.1

问题是您正在传递一个 list,它是可变的,但被测试的函数对它们有副作用。

一个可能的解决方案是将列表的 deepcopy 传递给测试函数。使用标准库中的 copy 模块。

例如

assert is_equal(result, union(copy.deepcopy(GRADUATES), copy.deepcopy(MANAGERS)))