You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$ cat -n test_bug1.py
1 def foo(x):
2 if x == 1:
3 return x
4 elif x == 2:
5 raise NotImplementedError
6
7
8 def test_foo():
9 foo(1)
$ pytest -q --cov test_bug1 test_bug1.py
. [100%]
---------- coverage: platform linux, python 3.10.0-final-0 -----------
Name Stmts Miss Branch BrPart Cover Missing
----------------------------------------------------------
test_bug1.py 6 1 2 1 75% 4
----------------------------------------------------------
TOTAL 6 1 2 1 75%
1 passed in 0.07s
IMHO, elif on the line 4 should be ignored together with an exception and the line 2 should be marked as partially covered: no 2->exit. Currently it reports (in the html), that there is not jump 2->4.
Thanks for these examples. I don't think these are bugs, but they are subtle. Here's my explanations:
test_bug1
In your code as written, the elif will not be excluded, because it could still be executed, for example if x == 3. If you change the elif to an else, as I did at line 12, then the else is exclude, and if x == 1 is not considered a partial line.
Similarly, here line 23 could still execute, if an exception other than TypeError is raised. An exception like that was not raised, so that line has never executed.
Here line 31 is partial because the comprehension in all(...) never completed (or started). The if statement has two exits to start with, and the comprehension adds more, which makes it hard to interpret the coverage results.
Just in case you miss this comment and below...
Example 1:
IMHO, elif on the line 4 should be ignored together with an exception and the line 2 should be marked as partially covered: no 2->exit. Currently it reports (in the html), that there is not jump 2->4.
Example 2:
Example 3:
All on the git version, after 9209c55 with config:
The text was updated successfully, but these errors were encountered: