coveragepy
coveragepy copied to clipboard
Coverage not reported in multiprocessing.Process
Describe the bug
When pytest launches a test using multiprocessing.Process, lines hit are not recorded as covered.
To Reproduce Here is a repo with a minimal reproducible example, along with a CI run showing coverage not being hit. https://github.com/ionite34/_demo_pytest_subprocess
Module file: https://github.com/ionite34/_demo_pytest_subprocess/blob/main/foo/mod.py
def add(a, b):
return a + b
def sub(a, b):
return a - b
Test file: https://github.com/ionite34/_demo_pytest_subprocess/blob/main/tests/test_mod.py
import pytest
from foo import mod
def test_add():
assert mod.add(1, 2) == 3
@pytest.mark.run_in_subprocess
def test_sub():
assert mod.sub(1, 1) == 0
Multiprocessing code for run_in_subprocess:
https://github.com/ionite34/_demo_pytest_subprocess/blob/main/tests/conftest.py
Expected behavior
the function sub is showed as not covered, as can be seen here
https://github.com/ionite34/_demo_pytest_subprocess/actions/runs/4015443879/jobs/6897178045#step:5:21
html artifact also shows line hit by subprocess as missing:
https://github.com/ionite34/_demo_pytest_subprocess/suites/10597630040/artifacts/528237599

Have you specified concurrency=multiprocessing in your configuration? https://coverage.readthedocs.io/en/7.2.1/config.html#run-concurrency
Creating a .coveragerc file like this makes everything measured correctly:
[run]
concurrency = multiprocessing
parallel = true
Then use coverage combine to combine the data, and the report includes all the code.