Search code examples
pytestdoctestpytest-cov

Pytest run tests and doctests with coverage


I want to run pytest with coverage (pytest-cov) for my set of tests (stored in ./test) + doctests (stored in ./myproj/*.py) in a single command but I seem to be unable to get it to work

Running the unittests separately and appending the results from doctest works fine e.g.

pytest --cov=myproj --cov-report=html --cov-report=xml --cov-context=test
pytest --cov=myproj --cov-report=html --cov-report=xml --cov-append --doctest-modules

for CI this is fine bug for local testing it is rather annoying to have to type the second command every time.

Attempting to combine the two commands e.g.

pytest --cov=myproj --cov-report=html --cov-report=xml --cov-context=test --doctest-modules myproj

only runs the doctests.

Am I missing something obvious?


  1. Similar issue (lacking an answer): Run both doctests and normal tests with Pytest
  2. Similar issue (solution not working): How to make pytest run doctests as well as normal tests directory?

Solution

  • After a bit of searching it dawned on me that I had to include both myproj and test directories and now coverage includes both tests and doctests

    pytest myproj test --cov=myproj --cov-report=html --cov-report=xml --cov-context=test --doctest-modules