【问题标题】:Doctest multiple files from one file从一个文件中对多个文件进行 Doctest
【发布时间】:2021-05-04 06:10:05
【问题描述】:

项目结构

components/
    A.py
    B.py

run_test.py

A.pyB.py 都有一些带有doctest 测试用例的功能。

如何通过仅运行 run_test.py 来运行 A.pyB.py 中的所有测试?

任何其他实现“在 A.py 和 B.py 中运行所有测试”的方法也将受到赞赏。

【问题讨论】:

    标签: python testing doctest


    【解决方案1】:

    应该更详细地阅读文档。

    1. 使用this 答案导入路径。
    2. 使用 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
        
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-11
      • 2020-03-19
      • 2015-10-19
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多