Skip to content

Commit

Permalink
Explain how to use coverage run (docs)
Browse files Browse the repository at this point in the history
Fixes #152
  • Loading branch information
bittner committed Feb 11, 2024
1 parent c7e705b commit 9a3b2f0
Showing 1 changed file with 75 additions and 42 deletions.
117 changes: 75 additions & 42 deletions docs/testcoverage.rst
Original file line number Diff line number Diff line change
@@ -1,21 +1,50 @@
Test Coverage
=============

You can integrate Coverage.py with behave-django to find out the test coverage of your code.
You can integrate `Coverage.py`_ with behave-django to find out the test coverage
of your code.

Dependencies
------------
There are two ways to do this. The simple (and obvious one) is via invocation
through the ``coverage`` CLI. Alternatively, you can integrate Coverage in the
``environment.py`` file of your BDD test setup as shown below.

At First, you should install Coverage.py dependency
Prerequisites
-------------

Obviously, you need to install Coverage to measure code coverage, e.g.

.. code-block:: bash
$ pip install coverage[toml]
Invoke via ``coverage``
-----------------------

Instead of using ``python manage.py``, you simply use
``coverage run manage.py`` to invoke your BDD tests, e.g.

.. code-block:: bash
$ pip install coverage
$ coverage run manage.py behave
Afterwards, you can display a coverage report in your terminal to understand
which lines your tests are missing, e.g.

.. code-block:: bash
$ coverage report --show-missing
.. tip::

A Django project setup with coverage configured in ``pyproject.toml`` and
executed by Tox is show-cased in the `Painless CI/CD Copier template for
Django`_.

Environment.py
--------------
Integrate via ``environment.py``
--------------------------------

In ``environment.py``, add the code snippet below in the ``before_all`` function to start measuring test coverage:
In ``environment.py``, add the code snippet below in the ``before_all`` function
to start measuring test coverage:

.. code-block:: python
Expand All @@ -37,50 +66,54 @@ You can save the coverage result on html format.
cov.save()
cov.html_report(directory="./cov")
You can check the test coverage on the web with the following command.

.. code-block:: bash
$ python -m http.server --directory ./cov
Warning for behave-django
-------------------------
Internally, the time before_all is executed seems to be later than the time when django loads the modules set in each app.
.. warning::

So sometimes it is necessary to reload django app's modules for accurate test coverage measurement.
Internally, the time ``before_all`` is executed seems to be later than the
time when django loads the modules set in each app.

Like this:
So sometimes it is necessary to reload django app's modules for accurate
test coverage measurement.

.. code-block:: python
Like this:

import inspect
import importlib
def reload_modules():
import your_app1
import your_app2
for app in [your_app1, your_app2]:
members = inspect.getmembers(app)
modules = map(
lambda keyval: keyval[1],
filter(lambda keyval: inspect.ismodule(keyval[1]), members),
)
for module in modules:
try:
importlib.reload(module)
except:
continue
.. code-block:: python
.. code-block:: python
import inspect
import importlib
def before_all(context):
# cov
cov = coverage.Coverage()
cov.start()
context.cov = cov
def reload_modules():
import your_app1
import your_app2
for app in [your_app1, your_app2]:
members = inspect.getmembers(app)
modules = map(
lambda keyval: keyval[1],
filter(lambda keyval: inspect.ismodule(keyval[1]), members),
)
for module in modules:
try:
importlib.reload(module)
except:
continue
.. code-block:: python
def before_all(context):
# cov
cov = coverage.Coverage()
cov.start()
context.cov = cov
# modules
reload_modules()
# modules
reload_modules()
.. _Coverage.py: https://coverage.readthedocs.io/
.. _Painless CI/CD Copier template for Django:
https://gitlab.com/painless-software/cicd/app/django

0 comments on commit 9a3b2f0

Please sign in to comment.