Code coverage
When running tests, the information of what part of the code has been tested is also meaningful. It is often referred to as code coverage.
With Python it can be generated with the coverage package, but also directly with pytest and pytest-cov.
To get the coverage output, run:
$ pytest --cov-report html --cov-report term-missing --cov=gitlabciintroduction
============================================================ test session starts =============================================================
platform linux -- Python 3.6.7, pytest-4.0.1, py-1.7.0, pluggy-0.8.0
rootdir: /home/user/gitlabciintroduction/python, inifile:
plugins: cov-2.6.0
collected 2 items
gitlabciintroduction/test/sphere_test.py .. [100%]
----------- coverage: platform linux, python 3.6.7-final-0 -----------
Name Stmts Miss Cover Missing
-------------------------------------------------------------
gitlabciintroduction/__init__.py 1 0 100%
gitlabciintroduction/sphere.py 18 6 67% 15, 26-31
gitlabciintroduction/test/__init__.py 0 0 100%
gitlabciintroduction/test/sphere_test.py 10 0 100%
-------------------------------------------------------------
TOTAL 29 6 79%
Coverage HTML written to dir htmlcov
========================================================== 2 passed in 0.05 seconds ==========================================================
You can read the coverage output directly in the pytest output. But a human readable html report is also generated. Look at the html report using a Web browser :
firefox htmlcov/index.html &
You can click on files to see covered parts (in green) and uncovered parts.
setup.py sub-commands
As running pytest with all the options is quite a pain, you will use setuptools to configure the pytest command.
Configuring setup.py commands requires adding options in a setup.cfg file. Copy the second script from the raw folder as setup.cfg.
cp raw/second setup.cfg
cat setup.cfg
The first part is for test configuration to use pytest. The second part are the options given to pytest. Run the command :
$ python setup.py test
...
-------- coverage: platform linux2, python 2.7.15-candidate-1 --------
Name Stmts Miss Cover Missing
-------------------------------------------------------------
gitlabciintroduction/__init__.py 1 0 100%
gitlabciintroduction/sphere.py 18 6 67% 15, 26-31
gitlabciintroduction/test/__init__.py 0 0 100%
gitlabciintroduction/test/sphere_test.py 10 0 100%
-------------------------------------------------------------
TOTAL 29 6 79%
Coverage HTML written to dir htmlcov
========================================================== 2 passed in 0.04 seconds ==========================================================
Now running the tests with python setup.py test also generates the coverage report.
Artifacts
In order to access the code coverage results, htmlcov
folder
has to be saved as an artifact.
Let's modify the .gitlab-ci.yml
file as follows :
stages:
- test
job_test:
stage: test
tags:
- linux
script:
- cd python
- python setup.py test
artifacts:
expire_in: 2 hrs
paths:
- python/htmlcov/
python/htmlcov/
folder is present in the
.gitignore
file.
coverage
GitLab CIGitLab CI allows coverage
Add the following regex in the job_test
item of the .gitlab-ci.yml
file:
coverage: '/\d+\%\s*$/'
Commit and push the .gitlab-ci.yml
file and check the job_coverage
succeds.
Check on the GitLab web site, in CI / CD
, Jobs
that the coverage percentage
is appearing.
Check you can download the artifact and check in a web browser the coverage.
Home | Python Home | << Python Previous - Refactoring | >> Python Next - Static analysis