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 c12d482
Showing 1 changed file with 67 additions and 42 deletions.
109 changes: 67 additions & 42 deletions docs/testcoverage.rst
Original file line number Diff line number Diff line change
@@ -1,21 +1,46 @@
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) via invocation
through the ``coverage`` CLI binary, and alternatively by coding the integration
in the ``environment.py`` file of your BDD test setup.

At First, you should install Coverage.py dependency
.. _Coverage.py: https://coverage.readthedocs.io/

Prerequisites
-------------

Obviously, you need to install Coverage.py 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
$ 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
$ pip install coverage
$ coverage report --show-missing
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 +62,50 @@ 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()

0 comments on commit c12d482

Please sign in to comment.