Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make pytest run doctests as well as normal tests directory?

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

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.

like image 904
Yang Avatar asked Sep 02 '25 16:09

Yang


2 Answers

Now it is implemented :-).

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

Man page: PyTest: doctest integration for modules and test files.

like image 172
dmitry_romanov Avatar answered Sep 04 '25 06:09

dmitry_romanov


This is how I integrate doctest in a pytest test file:

import doctest
from mylib import mymodule

def test_something():
   """some regular pytest"""
   foo = mymodule.MyClass()
   assert foo.does_something() is True

def test_docstring():
   doctest_results = doctest.testmod(m=mymodule)
   assert doctest_results.failed == 0

pytest will fail if doctest fails and the terminal will show you the doctest report.

like image 25
Bkyn Avatar answered Sep 04 '25 05:09

Bkyn