# anatolybobunov/pytest-api-coverage architecture

> Architecture diagram of the GitHub repository anatolybobunov/pytest-api-coverage, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/anatolybobunov/pytest-api-coverage
- Repository: https://github.com/anatolybobunov/pytest-api-coverage

## Overview

#### 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: [`src/pytest_api_coverage/reporter.py`](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/reporter.py) (Coverage reporter), [`src/pytest_api_coverage/orchestrator.py`](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/orchestrator.py) (Session orchestrator), [`src/pytest_api_coverage/collector.py`](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/collector.py) (Runtime collector), [`src/pytest_api_coverage/plugin.py`](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/plugin.py) (Pytest plugin hooks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_pytest["Pytest integration"]
  node_plugin["Pytest plugin hooks<br/>pytest boundary<br/>[plugin.py]"]
  node_orchestrator["Session orchestrator<br/>coverage coordinator<br/>[orchestrator.py]"]
  node_xdist["pytest-xdist workers<br/>parallel runtime"]
end

subgraph group_capture["HTTP capture"]
  node_adapter_contract["Adapter contract<br/>capture interface<br/>[base.py]"]
  node_requests_adapter["requests adapter<br/>sync interceptor"]
  node_httpx_adapter["httpx adapter<br/>sync/async interceptor<br/>[httpx_adapter.py]"]
  node_collector["Runtime collector<br/>request capture state<br/>[collector.py]"]
end

subgraph group_coverage["Coverage processing"]
  node_settings["Specification settings<br/>configuration model<br/>[settings.py]"]
  node_multi_spec["Multi-spec configuration<br/>configuration model<br/>[multi_spec.py]"]
  node_spec_sources["OpenAPI spec sources"]
  node_swagger["Swagger/OpenAPI parser<br/>spec loader and matcher<br/>[swagger.py]"]
  node_models["Coverage models<br/>domain representations<br/>[models.py]"]
end

subgraph group_output["Reporting"]
  node_reporter["Coverage reporter<br/>report data builder<br/>[reporter.py]"]
  node_terminal["Terminal output<br/>human report<br/>[terminal.py]"]
  node_json_writer["JSON writer<br/>persisted output<br/>[json_writer.py]"]
  node_csv_writer["CSV writer<br/>persisted output<br/>[csv_writer.py]"]
  node_html_writer["HTML writer<br/>persisted output<br/>[html_writer.py]"]
end

node_package["Package &amp; dependencies<br/>Python packaging<br/>[pyproject.toml]"]
node_ci["CI workflow<br/>automation<br/>[ci.yml]"]
node_publish["Publish workflow<br/>release automation<br/>[publish.yml]"]

node_package -->|"declares plugin runtime"| node_plugin
node_plugin -->|"lifecycle hooks"| node_orchestrator
node_orchestrator -->|"initializes capture"| node_adapter_contract
node_adapter_contract -->|"implemented by"| node_requests_adapter
node_adapter_contract -->|"implemented by"| node_httpx_adapter
node_requests_adapter -->|"captured requests"| node_collector
node_httpx_adapter -->|"captured requests"| node_collector
node_xdist -.->|"worker-local capture"| node_collector
node_plugin -->|"reads options"| node_settings
node_settings -->|"configures"| node_multi_spec
node_settings -->|"selects sources"| node_spec_sources
node_multi_spec -->|"defines specifications"| node_spec_sources
node_spec_sources -->|"loads"| node_swagger
node_collector -->|"runtime activity"| node_orchestrator
node_swagger -->|"declared operations"| node_orchestrator
node_orchestrator -->|"computes coverage"| node_models
node_models -->|"coverage result"| node_reporter
node_reporter -->|"renders console report"| node_terminal
node_reporter -->|"writes"| node_json_writer
node_reporter -->|"writes"| node_csv_writer
node_reporter -->|"writes"| node_html_writer
node_ci -.->|"tests"| node_package
node_publish -.->|"publishes"| node_package

click node_package "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/pyproject.toml"
click node_plugin "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/plugin.py"
click node_orchestrator "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/orchestrator.py"
click node_adapter_contract "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/adapters/base.py"
click node_requests_adapter "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/adapters/requests_adapter.py"
click node_httpx_adapter "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/adapters/httpx_adapter.py"
click node_collector "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/collector.py"
click node_settings "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/config/settings.py"
click node_multi_spec "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/config/multi_spec.py"
click node_swagger "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/schemas/swagger.py"
click node_models "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/models.py"
click node_reporter "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/reporter.py"
click node_terminal "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/terminal.py"
click node_json_writer "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/writers/json_writer.py"
click node_csv_writer "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/writers/csv_writer.py"
click node_html_writer "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/writers/html_writer.py"
click node_ci "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/.github/workflows/ci.yml"
click node_publish "https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/.github/workflows/publish.yml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_plugin,node_orchestrator,node_xdist toneBlue
class node_adapter_contract,node_requests_adapter,node_httpx_adapter,node_collector toneAmber
class node_settings,node_multi_spec,node_spec_sources,node_swagger,node_models toneMint
class node_reporter,node_terminal,node_json_writer,node_csv_writer,node_html_writer toneRose
class node_package,node_ci,node_publish toneNeutral
```

## Components

### Pytest integration

- [Pytest plugin hooks](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/plugin.py): pytest boundary · `src/pytest_api_coverage/plugin.py`
- [Session orchestrator](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/orchestrator.py): coverage coordinator · Coordinates setup, aggregation, and final output. · `src/pytest_api_coverage/orchestrator.py`
- **pytest-xdist workers**: parallel runtime · Workers maintain capture state for parallel test execution.

### HTTP capture

- [Adapter contract](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/adapters/base.py): capture interface · `src/pytest_api_coverage/adapters/base.py`
- [requests adapter](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/adapters/requests_adapter.py): sync interceptor · `src/pytest_api_coverage/adapters/requests_adapter.py`
- [httpx adapter](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/adapters/httpx_adapter.py): sync/async interceptor · `src/pytest_api_coverage/adapters/httpx_adapter.py`
- [Runtime collector](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/collector.py): request capture state · Records intercepted HTTP method and URL activity. · `src/pytest_api_coverage/collector.py`

### Coverage processing

- [Specification settings](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/config/settings.py): configuration model · Defines spec sources, origin filtering, and split behavior. · `src/pytest_api_coverage/config/settings.py`
- [Multi-spec configuration](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/config/multi_spec.py): configuration model · `src/pytest_api_coverage/config/multi_spec.py`
- **OpenAPI spec sources**: local files / remote URLs · JSON or YAML specifications may be loaded locally or over the network.
- [Swagger/OpenAPI parser](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/schemas/swagger.py): spec loader and matcher · Parses definitions and maps requests to declared operations. · `src/pytest_api_coverage/schemas/swagger.py`
- [Coverage models](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/models.py): domain representations · `src/pytest_api_coverage/models.py`

### Reporting

- [Coverage reporter](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/reporter.py): report data builder · Converts computed coverage into report-oriented data. · `src/pytest_api_coverage/reporter.py`
- [Terminal output](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/terminal.py): human report · `src/pytest_api_coverage/terminal.py`
- [JSON writer](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/writers/json_writer.py): persisted output · `src/pytest_api_coverage/writers/json_writer.py`
- [CSV writer](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/writers/csv_writer.py): persisted output · `src/pytest_api_coverage/writers/csv_writer.py`
- [HTML writer](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/src/pytest_api_coverage/writers/html_writer.py): persisted output · `src/pytest_api_coverage/writers/html_writer.py`

### Other components

- [Package & dependencies](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/pyproject.toml): Python packaging · `pyproject.toml`
- [CI workflow](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/.github/workflows/ci.yml): automation · `.github/workflows/ci.yml`
- [Publish workflow](https://github.com/anatolybobunov/pytest-api-coverage/blob/dev/.github/workflows/publish.yml): release automation · Publishes the package to PyPI. · `.github/workflows/publish.yml`

## Connections

- 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
