coverage doesn't find a non-covered string

Issue #459 invalid
Boris Nagaev
created an issue

Line 2 is not covered but it is not displayed in "misses".

$ cat -n foo.py 
     1  if 0:
     2      print(0)
     3  else:
     4      print(1)

$ coverage run foo.py 
1

$ coverage report
Name     Stmts   Miss  Cover
----------------------------
foo.py       1      0   100%

Comments (7)

  1. Ned Batchelder repo owner

    I think in this case the line has been compiled away because "if 0:" can be evaluated at compile time. If you change it to "if len([]):", what happens? If that's not the explanation, then can you show the output of "coverage debug sys"?

  2. Boris Nagaev reporter

    I think in this case the line has been compiled away because "if 0:" can be evaluated at compile time.

    I think, coverage report should not depend on optimizations like this. If a code was compiled away, it means that it is not covered.

    If you change it to "if len([]):", what happens?

    $ coverage report -m
    Name     Stmts   Miss  Cover   Missing
    --------------------------------------
    foo.py       3      1    67%   2
    

    If that's not the explanation, then can you show the output of "coverage debug sys"?

    https://gist.github.com/starius/07dbddd89b690dff1915

  3. Boris Nagaev reporter

    coverage.py does parse source code as well as byte code. Could you detect lines of source code not represented in byte code in function _raw_parse and mark them noncovered, please?

  4. Ned Batchelder repo owner

    Currently, I don't have the information to know about "if 0:", but I am in the middle of a big change that could make it possible. I wonder if most people would want to see it marked as not covered? After all, the idea is that coverage is showing you what parts of your runnable code are not being run by your tests. If you have product code inside an "if 0", then it is not runnable at all.

  5. Boris Nagaev reporter

    Probably Python optimizes out not only if 0: but also more complicated conditions which are always false. They can be errors (typo or misconfiguration). By not considering not runnable code as not covered, coverage.py prevents programmers from finding such errors.

    Test with if 0: was my first experience with coverage.py.

  6. Log in to comment