[PYTHON] [Note] Measure coverage when running Django unit tests

Purpose

In order to search for the part that the unit test implemented on Django does not cover, we introduce coverage and measure the coverage.

Verification

environment

Introduction

It's assumed that Django is already installed.

$ pip install coverage

measurement

Run all tests and make measurements. At this time, a .coverage file is generated.

$ coverage run --source=. --omit='*/tests*' manage.py test

At this time, in order to exclude any directory that aggregates unit tests, the --omit option is used to exclude the coverage measurement. See help for the types of measurement options.

$ coverage run --help

result

Output the measurement result.

$ coverage report -m
Name                                                 Stmts   Miss  Cover   Missing
----------------------------------------------------------------------------------
auth/models.py                                          61      7    89%   15, 28-30, 115-116, 119
...
config/__init__.py                                       0      0   100%
...
----------------------------------------------------------------------------------
TOTAL                                                  967    207    79%

The column names and their meanings are as follows. Missing is output by using the -m option.

Column name meaning
Name Refers to the target file name for coverage measurement.
Stmts Abbreviation for Statements.Refers to the number of lines of executable code.
Miss Refers to the number of lines that were not executed in Stmts.
Cover coverage(Coverage rate)Point to.
Missing Refers to the line number that was the target of Miss.

See help for the types of options when outputting results.

$ coverage report --help

It is also possible to generate reports in HTML, XML, JSON format, etc. For details, see [Document](https://coverage.readthedocs.io/en/coverage-5.3/cmd.html#html-annotation] -coverage-html).

Summary

By measuring coverage when running Django's unit tests, we visualized areas that weren't covered.

reference

Recommended Posts

[Note] Measure coverage when running Django unit tests
Measure Django application coverage with Coverage.py
Django note 4
Django Note 5
Django Note 1
Django note 3
Django note 2
Note that admin.py is not reflected immediately when running Django with WSGIDaemonProcess
Python unit tests