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?
To generate a HTML report for a Selenium test, we have to install a plugin with the command: pip install pytest-html. To generate the report, we have to move from the current directory to the directory of the Pytest file that we want to execute. Then run the command: pytest --html=report.
This plugin produces coverage reports. Compared to just using coverage run this plugin does some extras: Subprocess support: you can fork or run stuff in a subprocess and will get covered without any fuss. Xdist support: you can use all of pytest-xdist's features and still get coverage.
pytest enables you to create marks, or custom labels, for any test you like. A test may have multiple labels, and you can use them for granular control over which tests to run. Later in this tutorial, you'll see an example of how pytest marks work and learn how to make use of them in a large test suite.
If you wish to always add pytest-cov with pytest, you can use addopts under pytest or tool:pytest section. For example: A unfortunate consequence of coverage.py’s history is that .coveragerc is a magic name: it’s the default file but it also means “try to also lookup coverage configuration in tox.ini or setup.cfg ”.
That's why it works. On the other hand, pytest-cov is creating a single Coverage object and setting its sources, which get passed on to the xml_report, and so it strips that from all the file names before figuring out their package names.
This plugin provides a clean minimal set of command line options that are added to pytest. For further control of coverage use a coverage config file. For example if tests are contained within the directory tree being measured the tests may be excluded if desired by using a .coveragerc file with the omit option set:
pytest-cov 2.0 is using a new .pth file ( pytest-cov.pth ). You may want to manually remove the older init_cov_core.pth from site-packages as it’s not automatically removed.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With