This package installs a .pth file that enables the coverage.py process_startup feature in the Python prefix/virtualenv in subsequent runs, facilitating automatic coverage measurement for subprocesses. It is currently stable and actively maintained, with version 1.0 being the latest. The release cadence is as-needed for stability and compatibility with coverage.py.
pip install coverage-enable-subprocessVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to enable coverage for a Python subprocess. It involves installing `coverage-enable-subprocess`, creating a `.coveragerc` file, setting the `COVERAGE_PROCESS_START` environment variable, and then running a Python script as a subprocess. The installed `.pth` file ensures that `coverage.py`'s `process_startup()` function is called, initiating coverage collection in the subprocess.
When creating subprocesses with `subprocess.Popen`, explicitly pass `env=os.environ.copy()` or carefully merge environment variables, e.g., `subprocess_env = os.environ.copy(); subprocess_env.update({'MY_VAR': 'value'}); subprocess.Popen(..., env=subprocess_env)`.Always use `process.terminate()` (sends SIGTERM) and then `process.wait()` with a timeout. Only resort to `process.kill()` if `terminate` and `wait` fail. Example: `process.terminate(); try: process.wait(timeout=1.0) except subprocess.TimeoutExpired: process.kill()`.
Add `parallel = True` under the `[run]` section in your `.coveragerc` file. After all processes complete, use `coverage combine` to merge the individual data files.
Ensure that `COVERAGE_PROCESS_START` is set to the absolute path of your `.coveragerc` file before launching subprocesses. Verify the file exists and is readable.