1.1.1 Implementation Process
Preparatory work
First, before running the command, we need to install a package: coverage
The command is as follows:
PIP Install coverage # (PY-3-m pip install coverage)
After successful installation, successfully and other words will appear.
Implementing code Coverage commands
There are 3 of the commands involved, which are given here first, followed by the use of examples to do a detailed explanation:
The first command is to run your test script file, the second command prints the coverage report information in the console, the third command generates a Htmlcov folder in the sibling directory, and opens the index.html in the folder to view code coverage in a graphical manner.
coverage?run?xxx.py
Coverage report-m
Coverage HTML
- Code Coverage-Practical examples
For example: I want to test the code coverage of my local operate_calculate.py code, which can be done as follows:
Command one: Coverage run operate_calculate.py
Command two: Coverage report-m
Command three: Coverage html
As a result, a Htmlcov folder is generated, and the contents of the folder are as follows:
Open the index.html and you'll see beautiful test reports:
Click on the specific module to view specific information, such as Click calc.py:
The red part is the line of code that is not executed.
This is what Python uses coverage packages to achieve code coverage.
Python Statistics Unit test code coverage