2
votes

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 (-"-)
1
If you run with the coverage CLI directly, you can do this I think. coverage.readthedocs.io/en/coverage-5.4/#quick-startPaul H
You have more control if you split up the test running and the reporting. Why does pytest have to generate the reports?Ned Batchelder
@NedBatchelder Hi and thank you for the comment and perspective, it helps me think about this issue in a different way. Yes maybe that would be best, to just use coverage.py like Paul H suggested. --- One advantage though of using pytest is that i guess there will only be one pass, which would speed things up a bit. (That was an assumption on my part but i haven't been able to verity it)sunyata
It's not faster to use pytest to generate coverage reports. Run pytest under coverage, either with pytest-cov, or with coverage run -m pytest. Then generate coverage reports from the results.Ned Batchelder

1 Answers

4
votes

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.