Coverage of process spawned by pytest

59 Views Asked by At

I am trying to get coverage on a Python process spawned by pytest. Here are the steps I took:

  1. Create a sitecustomize.py module in my local site packages directory
#/home/Olumide/.local/lib/python3.10/site-packages/sitecustomize.py
import coverage
coverage.process_startup()
  1. Set the COVERAGE_PROCESS_START environment variable as follows export COVERAGE_PROCESS_START=True
  2. Run the test coverage run --rcfile=.coveragerc -m pytest tests/gui/test_screenshots.py where the current directory contains the the .coveragerc file
[run]
source = src/
parallel = True
relative_files = True
omit =
    **/tests/*

Note that the script tests/gui/test_screenshots.py launches an external python application that I want coverage on.

Unfortunately I'm still getting the warning:

/home/Olumide/repos/app/3.10_env/lib/python3.10/site-packages/coverage/control.py:887: CoverageWarning: No data was collected. (no-data-collected)
  self._warn("No data was collected.", slug="no-data-collected")

Update

Found a 53248 byte .coverage file (called .coverage.Ubuntu-22.5371.XcjyqXNx) in the directory from which I ran the test. I can generate an HTML report from this file via the command:

coverage html --data-file=.coverage.Ubuntu-22.5371.XcjyqXNx.

So it looks like I've got coverage! Oddly though, only the __init__.py files have coverage stats (100%). I wonder whether this is what the warning message meant.

1

There are 1 best solutions below

2
Ned Batchelder On

The docs say,

It examines the COVERAGE_PROCESS_START environment variable, and if it is set, begins coverage measurement. The environment variable’s value will be used as the name of the configuration file to use.

You need to use the path to your config file as the value of the environment variable.