coverage-instrumentation-pattern

Fix Python 3.12+ coverage by switching pytest to the sysmon backend.

Updated Apr 4, 2026
One-click install
npx skills add https://github.com/saintgo7/claude-skills --skill coverage-instrumentation-pattern
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: coverage-instrumentation-pattern
Source: https://github.com/saintgo7/claude-skills/tree/main/coverage-instrumentation-pattern
Command: npx skills add https://github.com/saintgo7/claude-skills --skill coverage-instrumentation-pattern

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Prevents misleading Python test coverage results when using pytest in Python 3.12+ with async frameworks like FastAPI and httpx, by selecting the correct coverage backend to avoid false missing lines.

Core Features & Use Cases

  • Backend comparison (ctrace vs sysmon): Identifies when the default coverage backend under-measures async call stacks and switches to PEP-669 sys.monitoring.
  • Fast activation via config or env: Supports enabling sysmon using the COVERAGE_CORE environment variable, .coveragerc, or pyproject.toml.
  • CI gate re-calibration guidance: Recommends adjusting cov-fail-under thresholds after changing the backend so coverage checks remain meaningful.

Use case: You add or modify tests, yet gateway/admin logic still shows unexpectedly low coverage because async routes and ASGI middleware aren’t fully captured by ctrace.

Quick Start

Run pytest with the sysmon backend by setting COVERAGE_CORE to sysmon, for example for your source directory: COVERAGE_CORE=sysmon pytest --cov=src/.

Frequently Asked Questions about coverage-instrumentation-pattern

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
Why does pytest --cov show missing lines for FastAPI async routes even when tests pass?

Pytest --cov shows missing lines for FastAPI async routes because the default ctrace backend under-measures async call stacks. Switching to the PEP-669 sys.monitoring sysmon backend captures the missing ASGI execution paths.

How do I fix inaccurate Python 3.12 async test coverage in pytest?

Fix inaccurate Python 3.12 async test coverage by enabling the sysmon backend via the COVERAGE_CORE environment variable, .coveragerc, or pyproject.toml before running pytest --cov.

Does pytest coverage work with httpx ASGI TestClient out of the box?

Pytest coverage with httpx ASGI TestClient often under-measures async middleware using the default ctrace backend. Enabling the PEP-669 sysmon backend ensures accurate coverage tracking for these async paths.

What is the COVERAGE_CORE sysmon backend used for in Python testing?

The COVERAGE_CORE sysmon backend activates PEP-669 sys.monitoring in Python 3.12+, replacing ctrace to accurately capture async execution paths and prevent false missing lines in coverage reports.

Should I adjust CI cov-fail-under thresholds after switching coverage backends?

Adjust CI cov-fail-under thresholds after switching coverage backends because transitioning from ctrace to sysmon reveals previously untracked async code, changing your overall coverage percentage.