Using pytest coverage and codecov.io with Python

Viewed 1372

When I run pytest --cov I get a coverage value of ~60%. The report generated on codecov.io after the code is pushed to github shows 100%. I cannot understand how the two are different, I thought the same command is being ran.

Codecov.io shows

Files       •   •   •   Coverage
 __init__.py    2   2   0   0   100.00%
 c_wrapper.py   5   5   0   0   100.00%
 fibonacci.py   13  13  0   0   100.00%
 tests/test_fibonnaci.py    39  39  0   0   100.00%
Folder Totals (4 files) 59  59  0   0   100.00%
Project Totals (4 files)    59  59  0   0   100.00%

but running

pytest --cov=fibonacci fibonacci/tests

shows

----------- coverage: platform linux, python 3.7.4-final-0 -----------
Name                                Stmts   Miss Branch BrPart  Cover
---------------------------------------------------------------------
fibonacci/__init__.py                   2      2      0      0     0%
fibonacci/c_wrapper.py                  5      5      0      0     0%
fibonacci/fibonacci.py                 13     13      6      0     0%
fibonacci/tests/test_fibonnaci.py      39      0      0      0   100%
---------------------------------------------------------------------
TOTAL                                  59     20      6      0    60%

Where is the difference coming from, which is correct?

2 Answers

As you can see at the pytest documentation the difference of using:

python -m pytest [...]

and

pytest [...]

is that "calling via python will also add the current directory to sys.path."

This could be the difference in the coverage. But, I believe that other plugins are being called before pytest-cov.

Pytest has an early-loading parameter -p which forces the plugin to be called before any other.

The command bellow could solve the cover issue.

pytest -p pytest_cov --cov

Also, for tests, you can try running the commands below to check which lines are not being covered and compare both:

pytest --cov-report term-missing --cov
python -m pytest --cov-report term-missing --cov

There seems to be (for reasons I don't understand but would like to) a difference between running

pytest --cov=fibonacci fibonacci/tests

and

python -m pytest --cov=fibonacci fibonacci/tests

Doing the later shows full coverage

Related