9

I have a little pet project in Python for which I want do do coverage reports. When I run

py.test -x -s MYPACKAGE --cov-report html --cov MYPACKAGE

It shows me a significant amount of lines missing from coverage. Mainly these are imports and class/method definitions. Screenshot

I am certain that all these lines are processed in my unit tests, and the lines 19 and 31-35 verify that.

Why does py.test mark all the definitions as "missing"?

Billal Begueradj
  • 20,717
  • 43
  • 112
  • 130
mawimawi
  • 4,222
  • 3
  • 33
  • 52
  • 1
    Can you try deleting `.pyc` files and re-running? I'm not very familiar with py.test but I've had experience with other test packages that make some optimizing assumptions that may not play well with other libraries. – turtlemonvh Mar 06 '14 at 15:00
  • Deleting the .pyc files did nothing to improve the situation. It's the same result as written in my question above. – mawimawi Mar 07 '14 at 15:48

2 Answers2

6

A frequent cause is that the module conftest.py imports early the module that should be measured. The test configuration should be evaluated before tests of course. That dependency can not be easily removed sometimes. That is why many answers recommend ways how to circumvent the pytest-cov extension:
answer 1 and answer 2

hynekcer
  • 14,942
  • 6
  • 61
  • 99
3

Why does py.test mark all the definitions as "missing"?

The coverage report is correct, because all of those lines are imported before the test actually starts.


I am certain that all these lines are processed in my unit tests, and the lines 19 and 31-35 verify that.

All first-class objects are evaluated on load, including imports, globals, functions definitions with their arguments, and class definitions with their methods, attributes, and arguments.

Lines 19 and 31-35 are processed as part of the unit test, but that doesn't mean the others are.

mhlester
  • 22,781
  • 10
  • 52
  • 75
  • 2
    But it hurts my coverage report. I never seem to be able to reach my goal of 100%. Any idea how to get it to report my desired 100%? – mawimawi Mar 08 '14 at 09:03