How anatolybobunov/pytest-api-coverage works
Purpose and Entry Points
pytest-api-coverage is a Python 3.11+ pytest plugin that measures exercised HTTP API operations against Swagger/OpenAPI specifications. Pytest loads the plugin entry behavior from src/pytest_api_coverage/plugin.py; orchestration is centralized in src/pytest_api_coverage/orchestrator.py. Packaging and pytest/plugin dependencies are defined in pyproject.toml.
Pytest Lifecycle Integration
The plugin registers CLI/configuration options, initializes coverage processing around a test session, and finalizes reporting after execution. src/pytest_api_coverage/plugin.py is the pytest boundary; src/pytest_api_coverage/orchestrator.py coordinates setup, collection, aggregation, and output. This keeps pytest hooks separate from coverage-domain logic.
HTTP Capture Adapters
Requests are intercepted at the client-library boundary rather than through an HTTP proxy. src/pytest_api_coverage/adapters/base.py defines the adapter contract, while src/pytest_api_coverage/adapters/requests_adapter.py and src/pytest_api_coverage/adapters/httpx_adapter.py implement interception for synchronous requests and synchronous/asynchronous httpx. Captured request metadata is passed into the collector.
Coverage Collection and Domain Model
src/pytest_api_coverage/collector.py receives intercepted HTTP method/URL activity and records which API operations were exercised. src/pytest_api_coverage/models.py holds the shared coverage/reporting representations. The collection boundary is important: adapters capture runtime traffic, while later stages interpret it against API definitions.
Specification Loading and Matching
OpenAPI/Swagger parsing is handled by src/pytest_api_coverage/schemas/swagger.py. Configuration for a single spec, URL/file loading, origin filtering, split-by-origin behavior, and multiple specifications is represented in src/pytest_api_coverage/config/settings.py and src/pytest_api_coverage/config/multi_spec.py. Specs may come from local JSON/YAML files or remote URLs; matching maps recorded requests to declared paths and methods.
Aggregation and Parallel Execution
The orchestrator combines collected runtime requests with parsed specifications to calculate covered versus uncovered operations. src/pytest_api_coverage/orchestrator.py is the aggregation/control point, and src/pytest_api_coverage/collector.py is the worker-side capture state. The project explicitly supports pytest-xdist, so collection/reporting must accommodate parallel pytest execution rather than assume one process.
Reporting and Output Boundaries
src/pytest_api_coverage/reporter.py converts computed coverage into report-oriented data. Human-facing terminal output is separated in src/pytest_api_coverage/terminal.py; persisted formats are isolated behind writers: src/pytest_api_coverage/writers/json_writer.py, src/pytest_api_coverage/writers/csv_writer.py, and src/pytest_api_coverage/writers/html_writer.py. This allows one coverage result to feed console, machine-readable, and HTML outputs independently.
Runtime and Delivery
The runtime is Python, pytest, and optional requests/httpx integrations; remote specification URLs are the principal external network dependency. The package is distributed through PyPI, with CI and publishing/version workflows under .github/workflows/ci.yml and .github/workflows/publish.yml.
Main entry points: reporter.py Coverage reporter · orchestrator.py Session orchestrator · collector.py Runtime collector · plugin.py Pytest plugin hooks
Components 20
Pytest integration
- Pytest plugin hooks
src/pytest_api_coverage/plugin.py - Session orchestrator · Coordinates setup, aggregation, and final output.
src/pytest_api_coverage/orchestrator.py - pytest-xdist workers · Workers maintain capture state for parallel test execution.
HTTP capture
- Adapter contract
src/pytest_api_coverage/adapters/base.py - requests adapter
src/pytest_api_coverage/adapters/requests_adapter.py - httpx adapter
src/pytest_api_coverage/adapters/httpx_adapter.py - Runtime collector · Records intercepted HTTP method and URL activity.
src/pytest_api_coverage/collector.py
Coverage processing
- Specification settings · Defines spec sources, origin filtering, and split behavior.
src/pytest_api_coverage/config/settings.py - Multi-spec configuration
src/pytest_api_coverage/config/multi_spec.py - OpenAPI spec sources · JSON or YAML specifications may be loaded locally or over the network.
- Swagger/OpenAPI parser · Parses definitions and maps requests to declared operations.
src/pytest_api_coverage/schemas/swagger.py - Coverage models
src/pytest_api_coverage/models.py
Reporting
- Coverage reporter · Converts computed coverage into report-oriented data.
src/pytest_api_coverage/reporter.py - Terminal output
src/pytest_api_coverage/terminal.py - JSON writer
src/pytest_api_coverage/writers/json_writer.py - CSV writer
src/pytest_api_coverage/writers/csv_writer.py - HTML writer
src/pytest_api_coverage/writers/html_writer.py
Other components
- Package & dependencies
pyproject.toml - CI workflow
.github/workflows/ci.yml - Publish workflow · Publishes the package to PyPI.
.github/workflows/publish.yml
Connections 23
- Package & dependencies → Pytest plugin hooks: declares plugin runtime
- Pytest plugin hooks → Session orchestrator: lifecycle hooks
- Session orchestrator → Adapter contract: initializes capture
- Adapter contract → requests adapter: implemented by
- Adapter contract → httpx adapter: implemented by
- requests adapter → Runtime collector: captured requests
- httpx adapter → Runtime collector: captured requests
- pytest-xdist workers → Runtime collector: worker-local capture
- Pytest plugin hooks → Specification settings: reads options
- Specification settings → Multi-spec configuration: configures
- Specification settings → OpenAPI spec sources: selects sources
- Multi-spec configuration → OpenAPI spec sources: defines specifications
- OpenAPI spec sources → Swagger/OpenAPI parser: loads
- Runtime collector → Session orchestrator: runtime activity
- Swagger/OpenAPI parser → Session orchestrator: declared operations
- Session orchestrator → Coverage models: computes coverage
- Coverage models → Coverage reporter: coverage result
- Coverage reporter → Terminal output: renders console report
- Coverage reporter → JSON writer: writes
- Coverage reporter → CSV writer: writes
- Coverage reporter → HTML writer: writes
- CI workflow → Package & dependencies: tests
- Publish workflow → Package & dependencies: publishes