🧪Test Coverage

Getting a Django Application to 100% Test Coverage

Introduction

Code coverage is a simple tool for checking which lines of our application code are run by our test suite. 100% coverage is a laudable goal, as it means every line is run at least once. Coverage.pyarrow-up-right is the Python tool for measuring code coverage. we can integrate it with a Django project.

Configuring Coverage.py

Install coverage:

pip install coverage

Checking the installation:

coverage --version

The default configuration file name is .coveragerc

[run]
branch = True
disable_warnings = no-data-collected
omit =
    # omit anything in a .local directory anywhere
    */.local/*
    */node_modules/*
    # omit everything in /migrations
    /migrations/*
    # omit this single file
    manage.py

[report]
exclude_lines =
    pragma: no cover
    def __repr__
    if self.debug:
    if settings.DEBUG
    raise AssertionError
    raise NotImplementedError
    if 0:
    if __name__ == .__main__.:

fail_under = 100
show_missing = True
skip_covered = True
ignore_errors = True

[html]
directory = coverage_html_report

There’s an integrated way of achieving this inside Django. We can patchmanage.pyto call Coverage.py’s APIarrow-up-right to measure when we run thetestcommand.

Run the test:

Last updated