API Reference — pytest Plugin¶
RAMPART's pytest integration. Activates automatically when installed.
_collection
¶
Result collection infrastructure for the pytest plugin.
Provides the ContextVar-based mechanism for collecting Result objects produced during test execution. The pytest plugin activates a collector per test; execution event handlers write into it automatically.
ResultCollectionHandler
¶
Bases: ExecutionEventHandler
Default ExecutionEventHandler installed on every BaseExecution.
Writes the Result into the active per-test collector on ON_POST_EXECUTE. No-op for all other events. No-op when no collector is active (safe to use outside pytest).
on_event
async
¶
Record result on post-execute. Ignore all other events.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event_data
|
ExecutionEventData
|
The event data. |
required |
Source code in rampart/pytest_plugin/_collection.py
ResultCollector
¶
record_result
¶
Record a Result into the active test's collector.
For building-block tests that construct Results manually rather than via Attacks. or Probes. factories. No-op when called outside a pytest test context (e.g., in library usage or scripts).
Re-exported from rampart at the top level — consumers import it as:
from rampart import record_result
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
result
|
Result
|
The result to record. |
required |
Source code in rampart/pytest_plugin/_collection.py
_session
¶
Session-scoped state for the RAMPART pytest plugin.
Accumulates Result objects, computes trial group aggregates, and builds the final TestRunReport.
Note: The architecture places RampartSession in plugin.py. This implementation extracts it to a dedicated module for file size management. This is a documented deviation from the architecture.
RampartSession
¶
Session-scoped state for the RAMPART plugin.
Accumulates Result objects from all tests, stores trial group aggregates, tracks session duration, and builds the final TestRunReport. Holds configured sinks for report emission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sinks
|
list[ReportSink]
|
Report sinks to emit to at session end. Defaults to an empty list (terminal-only output). |
None
|
Source code in rampart/pytest_plugin/_session.py
add_sinks
¶
Register additional sinks for report emission.
Called by the fixture-based bootstrap to add team-provided sinks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sinks
|
list[ReportSink]
|
Sinks to append. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If any item does not satisfy ReportSink. |
Source code in rampart/pytest_plugin/_session.py
set_duration
¶
Set the total session duration.
Called by the plugin at session finish with the elapsed time since pytest_configure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
duration_seconds
|
float
|
Total wall-clock seconds. |
required |
Source code in rampart/pytest_plugin/_session.py
| Python | |
|---|---|
absorb
¶
Absorb results from a completed test's collector.
Tags each result with the short test name (extracted from
the node ID) and the harm category from @pytest.mark.harm
so the terminal summary can group and display results.
Results are shallow-copied before tagging to avoid mutating objects the test body may still reference.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node
|
Item
|
The test item that just completed. |
required |
collector
|
ResultCollector
|
The test's result collector. |
required |
Source code in rampart/pytest_plugin/_session.py
record_trial_group
¶
Record aggregate statistics for a trial group.
Semantics:
- Any UNSAFE result across all trials -> group FAILS
- threshold is the minimum pass rate (SAFE / total).
e.g. 0.8 means at least 80% of runs must be SAFE.
- ERROR results count against the pass rate (they're not SAFE).
- Items with zero results (skipped or crashed before producing
a Result) are tracked as no_result and count against
the pass rate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base_nodeid
|
str
|
The original test's node ID. |
required |
trial_items
|
Sequence[Item]
|
All trial clone items. |
required |
threshold
|
float
|
Minimum pass rate required. |
required |
Source code in rampart/pytest_plugin/_session.py
build_report
¶
Build a TestRunReport from all collected results.
The report is cached and reused on subsequent calls. The cache is invalidated when new results are absorbed.
Returns:
| Name | Type | Description |
|---|---|---|
TestRunReport |
TestRunReport
|
Aggregated test run results. |