Having your tests in place after reading pytest Tutorial – Part 1 and pytest Tutorial – Part 2
you certainly want to know how much of your code is actually tested by your tests.
This is called coverage.
Table of Contents
Installation
pip install pytest-cov
Checking the coverage
pytest --cov=<module> test_<module>.py
Example
I wanted to analyze my python package flaskurls for test coverage. this is how you can do it:
pytest --cov=flask_url_mapping tests/
----------- coverage: platform win32, python 3.6.5-final-0 ----------- Name Stmts Miss Cover ----------------------------------------------------- flask_url_mapping\__init__.py 1 0 100% flask_url_mapping\flask_urls.py 73 0 100% ----------------------------------------------------- TOTAL 74 0 100% ========================== 10 passed in 0.60 seconds ==========================
HTML Reports
You can generate a nice html report from the test runs with
pytest --cov=flask_url_mapping tests/ --cov-report=html
SonarQube
If you want to use coverage analysis in sonarqube you can add
--cov-report=xml:./reports/coverage-report.xml