I've just started using Coverage.py module and so decided to make a simple test to check how it works.
Sample.py
def sum(num1, num2):
return num1 + num2
def sum_only_positive(num1, num2):
if num1 > 0 and num2 > 0:
return num1 + num2
else:
return None
test.py
from sample import sum, sum_only_positive
def test_sum():
assert sum(5, 5) == 10
def test_sum_positive_ok():
assert sum_only_positive(2, 2) == 4
def test_sum_positive_fail():
assert sum_only_positive(-1, 2) is None
As you see, all my code is covered with tests and py.test says all of them pass. I expect Coverage.py to show 100% coverage. Well, no.
Well, Coverage.py may not see test.py file, so I copied test functions to sample.py
file and ran Coverage again:
Then I added this block of code:
if __name__ == "__main__":
print(sum(2, 4))
print(sum_only_positive(2, 4))
print(sum_only_positive(-1, 3))
and removed all test functions. After that, Coverage.py shows 100%:
Why is it so? Shouldn't Coverage.py show code test coverage, not just execution coverage? I've read an official F.A.Q. for Coverage.py, but can't find the solution.
Since many SO users are familiar with code testing and code coverage, I hope you can tell me, where am I mistaken.
I have just one thought here: Coverage.py may simply watch which lines of code aren't executed so I should write tests for those lines. But there're lines which are executed already but aren't covered with tests so Coverage.py will fail here.