How to properly use in Python?

Python Programming

Question or problem about Python programming:

I’ve just started using module and so decided to make a simple test to check how it works.

def sum(num1, num2):
    return num1 + num2

def sum_only_positive(num1, num2):
    if num1 > 0 and num2 > 0:
        return num1 + num2
        return None

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 to show 100% coverage. Well, no.

Well, may not see file, so I copied test functions to 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, shows 100%:

Why is it so? Shouldn’t show code test coverage, not just execution coverage? I’ve read an official F.A.Q. for, 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: 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 will fail here.

How to solve the problem:

Solution 1:

Coverage looks for a .coverage file to read and generate that report for you. Py.test on its own does not create one. You need py.test plugin for coverage:

pip install pytest-cov

If you already have it, then you can run both at once like this:


Which means run test module and record/display coverage report on

If you need to have multiple test runs and accumulate their recorded coverage and then display a final report, you can run it like this:

py.test --cov-report=
py.test --cov-report=
py.test --cov-report=

Which means run test module and record (only) coverage on – don’t display a report.

Now you can run coverage command separately for a complete report:

coverage report -m

The command above simply displays a formatted coverage report based on the accumulated .coverage data file from previous test runs. -m means show lines missed i.e. lines not covered by tests:

Name        Stmts   Miss  Cover   Missing
-----------------------------------------       6      0   100%  

Coverage supports more switches like --include and --omit to include/exclude files using path patterns. For more info check out their docs:

Solution 2:

It’s a little hard to parse through your experiments, and you haven’t included the command lines you used with each experiment. But: if you run the tests with:

python -m py.test

then you can run them under with:

coverage run -m py.test

Solution 3:

The below command worked for me:

coverage run --source=sample -m pytest

coverage report -m

Hope this helps!