plugin
agent_cover.plugin
Pytest plugin for Agent Coverage integration.
This module implements the pytest hooks necessary to integrate Agent Coverage
into the test execution flow. It orchestrates the entire lifecycle:
1. Setup: Parses CLI options (--agent-cov, etc.).
2. Instrumentation: Initializes the AgentCoverage manager before tests start.
3. Teardown: Generates reports (JSON/XML/HTML) after tests finish.
CLI Options
The plugin adds the following flags to the pytest command line:
| Option | Description | Default |
|---|---|---|
--agent-cov |
Enable the plugin. Must be present to run instrumentation. | False |
--agent-cov-html=<dir> |
Directory to generate the static HTML report. | None |
--agent-cov-xml=<file> |
Path to generate the Cobertura XML report (CI/CD). | None |
--agent-cov-json=<file> |
Path to generate the raw JSON data dump. | agent_coverage.json |
--agent-source-dir=<dir> |
Root directory to scan for python modules (Discovery phase). | Current Working Dir |
--prompt-prefixes=<list> |
Comma-separated list of variable prefixes to scan as Raw Strings. | None |
--prompt-suffixes=<list> |
Comma-separated list of variable suffixes to scan as Raw Strings. | None |
--agent-cov-verbose |
Enable debug logging specifically for AgentCover internals. | False |
Hooks Implemented
pytest_addoption: Registers custom CLI flags.pytest_configure: Starts instrumentation and scans for static definitions.pytest_sessionfinish: Generates reports using data collected during the run.pytest_unconfigure: Clean up and stop instrumentation.
Classes
Functions
pytest_addoption(parser)
Registers the command-line options for agent coverage.
These options allow users to enable coverage, specify report paths, and customize heuristic scanning without changing code.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parser
|
The pytest parser object. |
required |
Source code in src/agent_cover/plugin.py
pytest_configure(config)
Initializes the AgentCoverage manager, loads the config, and applies patches.
This hook runs before any test is executed. It is responsible for:
1. Loading agent-cover.yaml.
2. Starting the Instrumentation.
3. Performing an initial static scan of the codebase to find Prompts and Tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
The pytest configuration object. |
required |
Source code in src/agent_cover/plugin.py
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 | |
pytest_sessionfinish(session, exitstatus)
Generates reports at the end of the session.
This hook runs after all tests have completed. It:
1. Runs a final discovery sweep (to catch dynamically defined objects).
2. Extracts execution data from the AgentRegistry.
3. Calls the reporting modules to generate JSON, XML, and HTML files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
The pytest session object. |
required | |
exitstatus
|
The exit status of the test run. |
required |
Source code in src/agent_cover/plugin.py
pytest_terminal_summary(terminalreporter, exitstatus, config)
Outputs a coverage summary to the terminal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
terminalreporter
|
The pytest terminal reporter object. |
required | |
exitstatus
|
The exit status of the test run. |
required | |
config
|
The pytest configuration object. |
required |
Source code in src/agent_cover/plugin.py
pytest_unconfigure(config)
Cleans up resources when the pytest session ends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
The pytest configuration object. |
required |