Test runner
test_runner
Agent-optimized test runner with structured output.
Wraps pytest with pytest-json-report to produce compact,
token-efficient results for AI coding agents.
FailureDetail
dataclass
Structured detail for a single test failure.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
error_type
instance-attribute
Exception class name, e.g. AssertionError.
file
instance-attribute
Relative file path.
line
instance-attribute
Line number of the assertion / raising statement.
message
instance-attribute
One-line error message.
test
instance-attribute
Full node ID, e.g. tests/unit/test_x.py::TestFoo::test_bar.
traceback
instance-attribute
Short traceback (truncated to _MAX_TB_LINES).
NonTestCauseDetail
dataclass
Serializable projection of a classified non-test cause.
TestReport is a dataclass whose payload is produced by
dataclasses.asdict: a pydantic model attached to it would survive that
traversal as an object and break every JSON consumer of the report. The
classifier remains the single source of the classification itself; this
only mirrors its verdict in the report's own payload shape.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
from_cause(cause)
classmethod
Return the projection of cause, or None when there is none.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
TestCase
dataclass
Lossless verdict for one collected pytest item.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
TestReport
dataclass
Compact test execution report.
All fields use None rather than empty containers when no data
exists so that dataclasses.asdict produces a minimal payload.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
__post_init__()
Populate additive execution metadata for directly-built reports.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
model_dump()
record_execution(*, return_code, collected, target_statuses)
Attach pytest evidence and compute the report's canonical verdict.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
build_pytest_cmd(*, report_path, coverage_path, files, markers, stop_on_first)
Build the pytest command line.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
build_test_report(*, report_data, total_cov, per_file_cov, mode=None, last_coverage=None, include_cases=False, return_code=0, stdout='', stderr='')
Build a TestReport from pytest JSON and coverage data.
Always parses failures and populates coverage — no mode branching.
The captured stdout/stderr and return_code are optional. When
they are supplied, the cause of a red that no test failure accounts for is
classified and attached to non_test_cause — explanatory only, it never
influences the verdict, and it stays None for a green run or a run red
by test failures.
Returns None for failures and coverage_by_file when no
data exists.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
parse_collector_errors(collectors)
Extract FailureDetail items from pytest-json-report collectors list.
Collector errors occur before test discovery completes (e.g.
SyntaxError in a test file, broken imports).
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
parse_coverage(coverage_path)
Parse coverage JSON into total % and per-file dict.
Files whose basename equals __main__.py are excluded from the
per-file map (they typically contain only a python -m entry
point and are not meaningfully unit-testable). The aggregate
total_pct from pytest-cov is left untouched, in line with
coverage.py's exclude_also convention of filtering reports
rather than rewriting the underlying totals.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
parse_failures(tests)
Extract FailureDetail items from pytest-json-report tests list.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
parse_json_report(report_path)
Read and parse a pytest-json-report JSON file.
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
run_tests(project_path, *, mode='failures', files=None, markers=None, stop_on_first=True, include_cases=False)
Run tests with agent-optimized structured output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
project_path
|
Path
|
Root of the project to test. |
required |
mode
|
str
|
Accepted for backward compatibility but ignored — all modes now produce the same unified output (failures + coverage). |
'failures'
|
files
|
list[str] | None
|
Specific test files or paths to run. |
None
|
markers
|
list[str] | None
|
Pytest markers to filter ( |
None
|
stop_on_first
|
bool
|
Stop on first failure ( |
True
|
Returns:
| Type | Description |
|---|---|
TestReport
|
Structured |
Source code in packages/axm-audit/src/axm_audit/core/test_runner.py
| Python | |
|---|---|
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 | |