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.
I had this problem with py.test, the coverage and the django plugin. Apparently the model files are imported before coverage is started. Not even "-p coverage" for early-loading of the coverage-plugin worked.
I fixed it (ugly?) by removing the models module from sys.modules and re-importing it in the test file that tests the model:
In my case, all the tests run, but coverage was 0%.
The fix was:
After the results were correct.
I had in past few problems with
py.test
command having problems to import something and setting thePYTHONPATH
env var was the solution. It worked for me this time too.My real example with
awslogs
First with
PYTHONPATH
unset:Resulting coverage is 0%.
Then I set the
PYTHONPATH
:and rerun the test:
Now is the coverage 90%.
WARNING: Manipulating
PYTHONPATH
can have strange side effects. Currently I run into problem, thatpbr
based package is creating egg directory when building distributable and ifPYTHONPATH
is set to ".", it automatically considers the egg related package as installed. For this reason I stopped usingpytest-cov
and follow the advice to usecoverage
tool instead.@hynekcer gave me the right idea. But basically the easiest solution lies somewhere else:
Get rid of
pytest-cov
!Use
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 andcoverage
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 withxdist
it can speed up your coverage reports.I fixed the test coverage to 94% by this patch that simplifies import dependencies and by the command:
Uncovered lines are only in conditional commands or in some less used functions but all headers are completely covered.
The problem was that the tests configuration
test/conftest.py
did import prematurely by dependencies almost all files in the project. The conftest file defines also additional command line options and settings that should be set before running the test. Therefore I think that pytest_cov plugin works correctly if it ignores everything that was imported together with this file, although it is a pain. I excluded also__init__.py
andsettings.py
from the report because they are simple and with the complete coverage but they rewe also imported prematurely in the dependency of conftest.