Skip to content

Commit

Permalink
coverage.py support for python unittests
Browse files Browse the repository at this point in the history
This commit brings coverage.py feature to python unittest runner. After
this change, you can run coverage.py with avocado for
avocado-instrumented and python unittest types.

Reference: #4957
Signed-off-by: Jan Richter <[email protected]>
  • Loading branch information
richtja committed May 31, 2024
1 parent abd6555 commit 02d642e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion avocado/plugins/runners/python_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,16 @@ def _run_unittest(cls, module_path, module_class_method, queue):
return

runner = TextTestRunner(stream=stream, verbosity=0)
unittest_result = runner.run(suite)
# running the actual test
if "COVERAGE_RUN" in os.environ:
from coverage import Coverage

coverage = Coverage(data_suffix=True)
with coverage.collect():
unittest_result = runner.run(suite)
coverage.save()
else:
unittest_result = runner.run(suite)

unittest_result_entries = None
if len(unittest_result.errors) > 0:
Expand Down
2 changes: 1 addition & 1 deletion docs/source/guides/writer/chapters/integrating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ like which parts are being exercised by the tests, may help develop new tests.
`Coverage.py`_ is a tool designed for measuring code coverage of Python
programs. It runs monitoring the program's source, taking notes of which
parts of the code have been executed. It is possible to use Coverage.py while
running Avocado Instrumented tests.
running Avocado Instrumented tests or Python unittests.

To make the Coverage.py parameters visible to other processes spawned by
Avocado, create the ``.coveragerc`` file in the project's root folder and set
Expand Down

0 comments on commit 02d642e

Please sign in to comment.