Examples
The examples/ directory contains a ready-to-run test suite that demonstrates
all three hierarchy report formats generated by a single pytest invocation.
Running the examples
cd examples
python -m pytest -v
This produces six files under examples/output/reports/:
| File | Captured at | Description |
|---|---|---|
collect_hierarchy.json/.xml |
pytest_collection_finish |
Pure structure — no outcomes or durations |
test_unexecuted_hierarchy.json/.xml |
pytest_collection_finish |
All tests initialised to "unexecuted" with @counts aggregated |
test_hierarchy.json/.xml |
pytest_sessionfinish |
Final outcomes, @duration, and @counts bubbled to every parent |
See the sub-pages for annotated output examples:
conftest.py walkthrough
The examples/conftest.py hooks into pytest to capture and write each snapshot:
from copy import deepcopy
from pytest import Session
from pytest_item_dict.plugin import ItemDictPlugin, ITEM_DICT_PLUGIN_NAME
def pytest_collection_finish(session: Session) -> None:
item_dict = session.config.pluginmanager.get_plugin(ITEM_DICT_PLUGIN_NAME)
if item_dict:
# 1. Pure structure — no outcomes
write_json_file(hierarchy=item_dict.collect_dict.hierarchy, prefix="collect")
write_xml_file(hierarchy=item_dict.collect_dict.hierarchy, prefix="collect")
# 2. All-unexecuted snapshot with aggregated @counts
snapshot = deepcopy(item_dict.test_dict.hierarchy)
item_dict.test_dict._aggregate_node(snapshot)
write_json_file(hierarchy=snapshot, prefix="test_unexecuted")
write_xml_file(hierarchy=snapshot, prefix="test_unexecuted")
def pytest_sessionfinish(session: Session) -> None:
item_dict = session.config.pluginmanager.get_plugin(ITEM_DICT_PLUGIN_NAME)
if item_dict:
# 3. Final outcomes + aggregated @counts (computed inside the plugin)
write_json_file(hierarchy=item_dict.test_dict.hierarchy, prefix="test")
write_xml_file(hierarchy=item_dict.test_dict.hierarchy, prefix="test")