【发布时间】:2025-12-16 15:20:05
【问题描述】:
我正在尝试跨多个模块进行自动化测试。所有这些模块都有一个带有单元测试的“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.
请帮我理解问题。
鼻子会是一个不错的选择吗?我不想使用它,因为我事先不知道模块是否使用 doctests、unittests 或简单语句。但是,如果这不是真的,请告诉我/您完全有另一种选择!
【问题讨论】:
标签: python unit-testing import automated-tests doctest