Ziva
Ziva

Reputation: 3501

Pytest cov does not generate any report

I'm trying to run a py.test cov for my program, but I still have an information: testFile.txt sCoverage.py warning: No data was collected. even when in the code are still non-tested functions (in my example function diff). Below is the example of the code on which I tested the command py.test --cov=testcov.py. I'm using python 2.7.9

def suma(x,y):
    z = x + y
    return z

def diff(x,y):
    return x-y

if __name__ == "__main__":
    a = suma(2,3)
    b = diff(7,5)
    print a
    print b

## ------------------------TESTS-----------------------------   
import pytest

def testSuma():
    assert suma(2,3) == 5

Can someone explain me, what am I doing wrong?

Upvotes: 10

Views: 18174

Answers (4)

Rexcirus
Rexcirus

Reputation: 2897

What worked well for me is:

py.test mytests/test_mytest.py --cov='.'

Specifying the path, '.' in this case, removes unwanted files from the coverage report.

Upvotes: 9

Soerendip
Soerendip

Reputation: 9148

py.test looks for functions that start with test_. You should rename your test functions accordingly. To apply coverage you execute py.test --cov. If you want a nice HTML report that also shows you which lines are not covered you can use py.test --cov --cov-report html.

Upvotes: 7

Ned Batchelder
Ned Batchelder

Reputation: 375574

You haven't said what all your files are named, so I'm not sure of the precise answer. But the argument to --cov should be a module name, not a file name. So instead of py.test --cov=testcov.py, you want py.test --cov=testcov.

Upvotes: 14

potfur
potfur

Reputation: 171

By default py.test looks for files matching test_*.py. You can customize it with pytest.ini

Btw. According to python style guide PEP 8 it should be test_suma - but it has no impact on py.test.

Upvotes: 0

Related Questions