26

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

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

4

4 回答 4

27

现在它已实施:-)。

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

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

手册页:PyTest:模块和测试文件的 doctest 集成。

于 2016-08-28T09:56:35.007 回答
1

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

于 2013-06-13T09:31:25.760 回答
0

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

于 2018-11-16T18:24:11.130 回答
0

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

  • pytest 仅在 /*test*.py AFAIK 中运行测试,您需要通过配置、testpaths 和 python_files 自定义此行为以在您的 python 代码文件中运行 doctest。另见: https ://docs.pytest.org/en/latest/example/pythoncollection.html#changeing-naming-conventions等等。
  • pytest 默认不运行 doctest,运行 pytest 时必须提供 --doctest-modules 选项。另见: https ://docs.pytest.org/en/stable/doctest.html
于 2020-07-17T00:14:28.563 回答