Search code examples
pythonpytestcode-coveragepytest-cov

How can I use pytest-cov to both generate a coverage report and also print to terminal?


Background

I'm new to using pytest and pytest-cov having switched over from unittest + coverage.py

I first set up my automated tests to run in this way:

python3 -m pytest --cov=myapplication

which gave me output like this to the terminal:

----------- coverage: platform linux, python 3.8.5-final-0 -----------
Name                        Stmts   Miss  Cover
-----------------------------------------------
myapplication/__init__.py       0      0   100%
myapplication/file.py          30     30     0%
myapplication/another_file.py  20      6    70%
[...]
-----------------------------------------------
TOTAL                        1195    464    61%

Then i wanted to generate an xml report so i changed the command:

python3 -m pytest --cov-report xml:coverage.xml --cov=myapplication

Problem

The problem i'm having is that after adding --cov-report xml:coverage.xml i no longer get any output to the terminal

Looking at the documentation for pytest-cov i find this:

These three report options output to files without showing anything on the terminal: [goes on to show xml, html and annotation reporting options]

Question

How can i both generate a report and also print to terminal in the same test run? (Is this even possible?)

(I could run the test suite two times, but if i can i'd like to do everything at once)


I am using these versions:

  • Python 3.8.5
  • pytest 6.2.2 (the latest version as of writing this)
  • pytest-cov 2.11.1 (-"-)

Solution

  • You can do this by specifying another --cov-report argument with one of the terminal output formats. You can have --cov-report term or --cov-report term-missing. For example:

    python3 -m pytest --cov-report term --cov-report xml:coverage.xml --cov=myapplication
    

    See the pytest-cov docs you linked to for how term and term-missing work.