如何让 pytest 运行 doctests 以及正常的测试目录?

问题描述:

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

We currently have pytest with the coverage plugin running over our tests in a tests directory.

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

What's the simplest way to also run doctests extracted from our main code? --doctest-modules doesn't work (probably since it just runs doctests from tests). Note that we want to include doctests in the same process (and not simply run a separate invocation of py.test) because we want to account for doctest in code coverage.

现在已经实现了 :-).

Now it is implemented :-).

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

To use, either run py.test --doctest-modules command, or set your configuration with pytest.ini:

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

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