Python/Testing/CodeCoverage: Difference between revisions
Jump to navigation
Jump to search
Brodriguez (talk | contribs) (Create page) |
(No difference)
|
Latest revision as of 02:31, 12 November 2022
Python CodeCoverage is a tool to measure UnitTest coverage for Python code.
It can be very useful for making sure your tests cover all relevant cases.
Note: Coverage (alone) is not necessarily all-encompassing.
For example, it's possible to technically have "100% code coverage" (in that all lines are touched at least once), yet not actually be testing for important edge cases, etc.
Installation
Using the Python environment of choice, run:
pip install coverage
Running Code Coverage
The base command is:
coverage run --source='<relative_path>' --omit='<relative_path>' -m <entity_to_run>
Where:
entity_to_run- Indicates the testing method to execute with.- Ex:
manage.py testorpytest.
- Ex:
source- The path to the directory to recursively check coverage within.- Usually, this is the path to project root, such as
--source='.'.
- Usually, this is the path to project root, such as
omit- The path to sub-directories ofsourceto exclude.- Ex: There's no point in checkout our environment so we can add
--omit='.venv/*'.
- Ex: There's no point in checkout our environment so we can add
For example, a full example using Pytest (to check coverage at project root, and exclude only the venv folder):
coverage run --source='.' --omit='.venv/*' -m pytest
Viewing Coverage Results
Running Code Coverage will only generate results to files, not necessarily show them.
View basic results with:
coverage report
Optional useful args:
-m- Display line numbers of missing coverage for each file.--skip-coveredSkip output for any files at 100% coverage. Particularly useful for large projects with good coverage already.
Ex:
coverage report -m --skip-covered
Generate result as html with:
coverage html
Optional useful args:
-d- Allows specifying a directory to generate html files to.