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

1

21

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 (-"-)
Parsaye answered 19/2, 2021 at 15:32 Comment(4)
If you run with the coverage CLI directly, you can do this I think. coverage.readthedocs.io/en/coverage-5.4/#quick-startDetinue
You have more control if you split up the test running and the reporting. Why does pytest have to generate the reports?Toner
@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)Parsaye
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.Toner
A
37

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.

Aluminothermy answered 19/2, 2021 at 15:48 Comment(1)
Once you have something like --cov-report xml:coverage.xml in your pyproject.toml it seems you're stuck with it. I'd like to have both reports in pyproject.toml so that my manual command line is pytest but still be able to specify only --cov-report term in the build pipeline. But pytest respects both the pyproject.toml and the command line as merged sources and does not over-ride the pyproject.toml when only one --cov-report is specified on the command line.Monika

© 2022 - 2024 — McMap. All rights reserved.