nedbat / coveragepy

The code coverage tool for Python
https://coverage.readthedocs.io
Apache License 2.0
2.98k stars 429 forks source link

Coverage not reported in multiprocessing.Process #1545

Open ionite34 opened 1 year ago

ionite34 commented 1 year ago

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

image
olivierlefloch commented 1 year ago

Have you specified concurrency=multiprocessing in your configuration? https://coverage.readthedocs.io/en/7.2.1/config.html#run-concurrency