Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using py.test with coverage doesn't include imports

For Jedi we want to generate our test coverage. There is a related question in stackoverflow, but it didn't help.

We're using py.test as a test runner. However, we are unable to add the imports and other "imported" stuff to the report. For example __init__.py is always reported as being uncovered:

Name                           Stmts   Miss  Cover -------------------------------------------------- jedi/__init__                      5      5     0% [..] 

Clearly this file is being imported and should therefore be reported as tested.

We start tests like this [*]:

py.test --cov jedi 

As you can see we're using pytest-coverage.

So how is it possible to properly count coverage of files like __init__.py?

[*] We also tried starting test without --doctest-modules (removed from pytest.ini) and activate the coverage module earlier by py.test -p pytest_cov --cov jedi. Neither of them work.

I've offered a bounty. Please try to fix it within Jedi. It's publicly available.

like image 258
Dave Halter Avatar asked May 06 '13 18:05

Dave Halter


People also ask

Does coverage work with pytest?

You can use Coverage.py with both unittest and Pytest.

Does pytest use coverage py?

Using coverage.py Some test runners provide coverage integration to make it easy to use coverage.py while running tests. For example, pytest has the pytest-cov plugin. You can fine-tune coverage. py's view of your code by directing it to ignore parts that you know aren't interesting.

What is py test Command?

In general, pytest is invoked with the command pytest (see below for other ways to invoke pytest). This will execute all tests in all files whose names follow the form test_*. py or \*_test.py in the current directory and its subdirectories. More generally, pytest follows standard test discovery rules.

How is coverage calculated Python?

Coverage.py counts the total number of possible executions. This is the number of executable statements minus the number of excluded statements. It then counts the number of those possibilities that were actually executed. The total percentage is the actual executions divided by the possible executions.


1 Answers

@hynekcer gave me the right idea. But basically the easiest solution lies somewhere else:

Get rid of pytest-cov!

Use

coverage run --source jedi -m py.test coverage report 

instead!!! This way you're just running a coverage on your current py.test configuration, which works perfectly fine! It's also philosophically the right way to go: Make each program do one thing well - py.test runs tests and coverage checks the code coverage.

Now this might sound like a rant, but really. pytest-cov hasn't been working properly for a while now. Some tests were failing, just because we used it.


As of 2014, pytest-cov seems to have changed hands. py.test --cov jedi test seems to be a useful command again (look at the comments). However, you don't need to use it. But in combination with xdist it can speed up your coverage reports.

like image 161
Dave Halter Avatar answered Sep 18 '22 16:09

Dave Halter