python - 从一个文件中对多个文件进行 Doctest
问题描述
项目结构
components/
A.py
B.py
run_test.py
A.py
每个都有B.py
一些带有 doctest 测试用例的功能。
如何运行所有测试A.py
并B.py
仅运行run_test.py
?
任何其他实现“在 A.py 和 B.py 中运行所有测试”的方法也将受到赞赏。
解决方案
应该更详细地阅读文档。
- 将此答案用于使用路径导入。
- 使用 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
推荐阅读
- reactjs - 找不到模块无法解析反应滚动到底部
- git - 尝试将 Gitlab 运行器合并到远程分支时出现验证错误
- java - Infinispan 查询 12.1.x 未检索到任何结果
- flutter - 在颤动中单击时仅在 ListTile 中的一项上显示尾随图标
- highcharts - Highcharts:当数据存储在 Google 电子表格中时,如何使 startRow 和 endRow 再次工作?
- python - 我可以(安全地)覆盖服务器端 wsgi.py 中的 Django 设置吗?
- jenkins - 配置 LDAP 时的 Jenkins CLI
- oop - 我们应该在拦截设计模式实现中使用 NullValueFilter 吗?
- react-native - 每次都没有加载远程 JSON 文件 - React Native
- sas - 如何根据变量的最大值打印 SAS 记录