如何通过unittest.main()选择测试用例调用的顺序

时间:2011-08-06 14:47:47

标签: python unit-testing

romantest.pyDive into Python: Introducing romantest.py

Dive into Python: Test-First Programming -

$ python romantest.py -v

fromRoman should only accept uppercase input ... ERROR
toRoman should always return uppercase ... ERROR
fromRoman should fail with malformed antecedents ... FAIL
fromRoman should fail with repeated pairs of numerals ... FAIL
fromRoman should fail with too many repeated numerals ... FAIL
fromRoman should give known result with known input ... FAIL
toRoman should give known result with known input ... FAIL
fromRoman(toRoman(n))==n for all n ... FAIL
toRoman should fail with non-integer input ... FAIL
toRoman should fail with negative input ... FAIL
toRoman should fail with large input ... FAIL
toRoman should fail with 0 input ... FAIL

[... snipped ...]

我无法理解调用顺序。它是如何由unittest.main()决定的?

2 个答案:

答案 0 :(得分:3)

根据unittest documentation

  

请注意,运行各种测试用例的顺序是通过根据字符串的内置顺序对测试函数名称进行排序来确定的。

答案 1 :(得分:2)

大多数情况下,你应该避免考虑测试的顺序。您需要编写每个测试,以使其完全独立于其他测试。那你就不关心测试的顺序了。

不同的测试跑步者可以按不同的顺序运行测试。

正如David指出的那样,unittest按功能名称顺序运行它们。我不确定测试类的运行顺序。