问题描述
项目结构
components/
A.py
B.py
run_test.py
A.py
和 B.py
每个都有一些带有 doctest 测试用例的函数。
如何通过仅运行 A.py
来运行 B.py
和 run_test.py
中的所有测试?
任何其他实现“在 A.py 和 B.py 中运行所有测试”的方法也将受到赞赏。
解决方法
应该更详细地阅读文档。
- 使用 this 答案导入路径。
- 使用来自 doctest 文档运行测试的示例
import doctest
import importlib.util
# from link
def import_module(name,path):
spec = importlib.util.spec_from_file_location(name,path)
foo = importlib.util.module_from_spec(spec)
spec.loader.exec_module(foo)
return foo
if __name__ == "__main__":
test_modules = [
("components.A","components/A.py"),("components.B","components/B.py")
]
for name,path in test_modules:
doctest.testmod(import_module(name,path)) # from document