【问题标题】:How to make pytest run doctests as well as normal tests directory?如何使 pytest 运行 doctests 以及正常的测试目录?
【发布时间】:2020-07-17 08:14:28
【问题描述】:

我们目前有 pytest,覆盖插件在 tests 目录中的我们的测试中运行。

同时运行从我们的主代码中提取的文档测试的最简单方法是什么? --doctest-modules 不起作用(可能是因为它只是从 tests 运行文档测试)。请注意,我们希望在同一进程中包含 doctest(而不是简单地运行 py.test 的单独调用),因为我们希望在代码覆盖率中考虑 doctest。

【问题讨论】:

    标签: python testing pytest doctest


    【解决方案1】:

    现在已经实现了:-)。

    要使用,请运行py.test --doctest-modules 命令,或使用pytest.ini 设置您的配置:

    $ cat pytest.ini
    # content of pytest.ini
    [pytest]
    addopts = --doctest-modules
    

    手册页:PyTest: doctest integration for modules and test files.

    【讨论】:

    • 这对我们不起作用,我们想指定测试在 mypackage/tests 和 doctests 例如在mypackage/foo起作用的是pytest mypackage/tests --doctest-modules mypackage/foo。在pytest.ini 中如何定义它?我试图在那里表达这一点,但没有雪茄。
    • @PierreD 感谢您分享您的解决方法。没有这样的文件结构。几个想法:一旦你有了工作命令行,我会尝试将它移动到pytest.ini,比如addopts = --doctest-modules mypackage/foo,看看它是否有效。我会使用 `--doctest-glob="foo/*.txt" 来尝试捕获您的文件。如果您提供一个带有文件结构 + requirements.txt 的 git-repo,以及您想要的结果,我希望可以更快地为您的情况修复它。
    • 好吧,我的评论是错误的;它做了我想要的,但出于错误的原因:测试和文档测试都是从两个路径中收集的。我能够在我们的 Makefile 中进一步调整 pytest 调用,但它并不能完全满足我的要求(我们也有集成测试和集成测试)。最重要的是,我需要一种方法来告诉 pytest 从可能的两组不同目录中收集 doctest 和常规测试集合。例如:所有内容的文档测试 (./mypackage),以及仅来自非集成测试的测试 (./mypackage/tests)。
    【解决方案2】:

    您可以尝试使用 pytest 的 repo 版本并粘贴会话日志吗?我认为--doctest-modules 应该选择任何 .py 文件。

    【讨论】:

      【解决方案3】:

      在一个模块中使用 doctest 以及普通测试。对于要提取的非 doctest 测试,标准 py.test 发现机制适用:带有测试前缀的模块名称,带有测试前缀的测试函数。

      【讨论】:

        【解决方案4】:

        看起来很老的问题,但把我的答案放在这里以防万一。

        【讨论】:

          猜你喜欢
          • 2021-05-05
          • 2012-07-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-01-13
          • 1970-01-01
          • 2023-03-24
          相关资源
          最近更新 更多