feat: collect coverage data from Python subprocesses#1055
Open
ctcjab wants to merge 1 commit into
Open
Conversation
When coverage is enabled, propagate the configuration to subprocesses so that pytest-xdist workers, subprocess.Popen calls, and other child processes sharing the same venv automatically measure coverage. At setup time, write a .pth file into the venv's site-packages so every subprocess calls coverage.process_startup() at interpreter init, and set COVERAGE_PROCESS_CONFIG with the serialized config. At teardown, save() + combine() merges per-process data files before generating the LCOV report. Requires coverage >= 7.10.3 for CoverageConfig.serialize(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
When coverage is enabled (
bazel coverage), propagate the configuration to Python subprocesses so that pytest-xdist workers,subprocess.Popencalls, and other child processes sharing the same venv automatically measure coverage.Problem:
pytest_main.pyonly collects coverage from the in-process test code. Code executing in subprocesses (e.g. pytest-xdist workers, or tests that spawn Python subprocesses) is invisible to coverage.py, which can causeNoDataErrorcrashes or incomplete coverage reports.Fix (~15 lines in
pytest_main.py):.pthfile to the venv's site-packages so every subprocess callscoverage.process_startup()at interpreter init. SetCOVERAGE_PROCESS_CONFIGwith the serialized config. Usedata_suffix=Trueso per-process data files don't collide.save()+combine()merges all per-process data files before generating the LCOV report.Also bumps
coverageminimum to>=7.10.3forCoverageConfig.serialize().Scope
This covers subprocesses that share the test's venv (pytest-xdist workers, same-venv subprocess calls). Cross-binary coverage (subprocess of a separate Bazel binary with its own venv) remains unsupported — that would require changes to binary build rules.
Test plan
coverage_lcov_shape_testpasses (no regression)subprocess_coverage_lcov_test— callsfoo.subtract()in a subprocess, asserts the LCOV containsDA:5,<nonzero>proving subprocess coverage was captured//examples/pytest/...tests pass (10/10)🤖 Generated with Claude Code