我正在尝试跨多个模块进行自动化测试。所有这些模块都有一个带有单元测试的“test()”函数。一些模块是基本的,它们的测试包含简单的语句,但大多数模块都有 unittest 或 doctest。我在动态导入和运行 doctest 时遇到了最大的麻烦。
例如,这里有一个模块sample.py
class sample:
"""
>>> import sample
>>> print sample.hello()
Hello
"""
def hello():
return "Hello"
def test():
import doctest
doctest.testmod(name=__name__, verbose=True)
这是我的文件 run_all_tests.py:
# assume I already have a list of all my files to test
for file in all_my_files:
temp_module = __import__(file)
temp_module.test()
这不起作用,我总是收到此错误:
1 items had no tests:
sample
0 tests in 1 items.
0 passed and 0 failed.
Test passed.
请帮助我理解问题。
Nose 会是一个不错的选择吗?我不想使用它,因为我不会事先知道模块是否使用 doctests、unittests 或简单语句。但是,如果这不是真的,请告诉我/您完全有另一种选择!
请您参考如下方法:
使用doctest.DocTestSuite .它需要一个模块,提取那里存在的所有 doctests,并将其作为 unittest.TestSuite 返回.然后,运行测试是小菜一碟。您的代码将如下所示:
for f in all_my_files:
temp_module = __import__(f)
test_suite = doctest.DocTestSuite(temp_module)
unittest.TextTestRunner().run(test_suite)
为什么你的代码不工作
来自doctest的testmod documentation :
Test examples in docstrings in functions and classes reachable from module m (or module __main__ if m is not supplied or is None), starting with
m.__doc__.
因此,由于您省略了第一个参数 (m),模块 __main__ 被传递给 testmod。因此,运行的 doctests 是包含 for 循环的模块中的 doctests。你可以自己看看:
run_tests.py
"""
>>> print 'oops'
oops
"""
# assume I already have a list of all my files to test
for file in all_my_files:
temp_module = __import__(file)
temp_module.test()
如果您现在运行示例(在修复它之前),您将看到:
Trying:
print 'oops'
Expecting:
oops
ok
1 items passed all tests:
1 tests in sample
1 tests in 1 items.
1 passed and 0 failed.
Test passed.
清楚地表明正在运行的 doctests 是 run_tests.py 中的那些。 name 参数仅更改出现在消息中的名称(示例)。
