加载鼻子和单元测试的测试套件的协议是什么?

时间:2019-05-07 02:21:42

标签: python unit-testing nose doctest

我使用了unittest load_tests()协议,因为我希望我的自动化测试包括一个doctest,但仅包含一个模块。当被unittest发现并运行时,此方法工作正常,但当由nose运行时,则失败。

import doctest
import unittest

import my.module

suite = doctest.DocTestSuite(my.module)

def load_tests(loader, std, pat): # invoked by unittest discovery process
    return suite

错误是:

Traceback (most recent call last):
  File "/g/data/w85/brl654/conda/envs/haz/lib/python2.7/site-packages/nose/case.py", line 197, in runTest
    self.test(*self.arg)
TypeError: load_tests() takes exactly 3 arguments (0 given)

如果我将这些参数设为可选,nose是否可以运行测试?是否有更好的方法来集成此测试套件,以便可以通过unittestnose运行它?

1 个答案:

答案 0 :(得分:0)

困难之处在于,nosetests通常会搜索unittest.TestCase子类和具有类似测试名称的函数的组合。 unittest.TestSuite既不是TestCase的子类也不是函数,因此不能自然地被鼻子所接受。 (此外,与unittest的测试加载协议相反,鼻子错误地拿起load_tests并尝试将其作为测试功能运行 。)

粗略的解决方案是:

load_tests.__test__ = False # instruct nose to skip load_tests()
def test_with_nose():
    assert suite.run(unittest.TestResult()).wasSuccessful()