Advanced: customize the export with hooks¶
A hook is a Python function pytest calls at a particular point in a run. You do
not need one for the tutorial. If you need custom execution metadata or evidence,
create conftest.py beside test_example.py:
import pytest
from pytest_xray import evidence
def pytest_xray_results(results, session):
results["info"]["revision"] = "tutorial-revision"
@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_makereport(item, call):
outcome = yield
report = outcome.get_result()
if report.when == "call":
report.evidences = getattr(report, "evidences", []) + [
evidence.text(data="synthetic evidence", filename="example.txt")
]
python -m pytest -q test_example.py --jira-xray --xraypath=xray.json --allow-duplicate-ids --xray-parameter-details
Expected: 2 passed. xray.json contains info.revision: "tutorial-revision"
and two evidence entries, one per case, on the combined Test result. The evidence
text is encoded in the payload. These are Test Run attachments, not per-iteration
attachments. cases.json is not written by this command.
When a case maps to multiple Jira Test keys, each key keeps its own evidence and
defect lists. For example, case 1 mapped to SYN-1 and SYN-2 with BUG-1, followed
by case 2 mapped only to SYN-1 with BUG-2, gives SYN-1 both cases' attachments
and defects; SYN-2 receives only case 1's. Two parameter cases both mapped to both
keys produce two attachment occurrences per key, even when their filenames and
contents are identical. Defect identifiers are still de-duplicated per key.
Version 0.1.4 fixes list sharing that could leak later attachments/defects into an
unrelated key or inflate attachment counts. It changes neither status precedence
nor comments, and needs no new flags. Use --allow-duplicate-ids when multiple
cases reuse a key. The fix applies to serial and xdist file exports and the same
payload used for upload; live server behavior has not been revalidated.
pytest_xray_results edits the final legacy payload before archival and comment
bounds are applied. The report hook attaches evidence after pytest creates each
call report. Hooks execute trusted local Python and can change correctness or
expose data; use only the fields and content you intend to export. Delete this
optional conftest.py when finished with the example.