pytest-harvest
pytest-harvest is a pytest plugin that allows you to store data created during your pytest tests execution and retrieve it at the end of the session. This is particularly useful for applicative benchmarking purposes, collecting custom metrics, or generating reports. The current version is 1.10.5, and it maintains a regular release cadence, often to ensure compatibility with new pytest versions and address bug fixes.
Common errors
-
AttributeError: 'str' object has no attribute 'iter_parents'
cause Incompatibility between older `pytest-harvest` versions and `pytest>=8.1` due to internal `pathlib` API changes.fixUpgrade `pytest-harvest` to version `1.10.5` or higher: `pip install --upgrade pytest-harvest`. -
ModuleNotFoundError: No module named 'pandas'
cause Attempting to use `module_results_df` or `session_results_df` fixtures when `pandas` is not installed.fixInstall the `pandas` library: `pip install pandas`. -
pytest-xdist: results not consolidated or missing across workers/master node
cause Issues with `pytest-xdist` hook execution or data transfer, especially in older `pytest-harvest` versions or specific Python/xdist combinations.fixEnsure `pytest-harvest>=1.10.4` is installed. Verify `pytest-xdist` is also up-to-date and compatible with your `pytest` version. Check `pytest_sessionfinish` hook for data retrieval. -
KeyError: 'custom_data_field' or attribute error when accessing collected data
cause Incorrectly accessing collected data via `request.node.add_pytest_stats` or `request.node.add_report_section` where the key/attribute name does not match retrieval logic in `get_session_synthesis_dct` or result fixtures.fixWhen using `request.node.add_pytest_stats(key=value)`, data is accessible as `result_object.key`. For `request.node.add_report_section('call', 'category', value)`, access might involve parsing `result_object.sections`.
Warnings
- breaking pytest-harvest versions prior to 1.10.5 are incompatible with pytest>=8.1 due to changes in pytest internals, leading to `AttributeError` or signature mismatches.
- gotcha Using result fixtures like `module_results_df` or `session_results_df` without `pandas` installed will raise an `ImportError`. `pytest-harvest` uses lazy loading for `pandas`.
- gotcha `pytest-xdist` integration, especially with older `pytest` versions or `python 3.5`, had known issues with result consolidation or `pathlib` object handling.
- gotcha Test IDs containing `::` were not properly handled by `get_session_synthesis_dct` and related fixtures, leading to incomplete or incorrect results.
- gotcha When using `pytest-cases` with `lazy_value` parameters, `module_results_df` in `pytest < 5.3` might wrongly insert values as integers instead of objects.
Install
-
pip install pytest-harvest
Imports
- get_session_synthesis_dct
from pytest_harvest import get_session_synthesis_dct
- module_results_dct
def test_something(module_results_dct):
- session_results_df
def test_something(session_results_df):
- saved_shared_data
def test_something(saved_shared_data):
Quickstart
import pytest
from pytest_harvest import get_session_synthesis_dct
# conftest.py
@pytest.fixture
def custom_metric_value():
return 'important_data'
def pytest_sessionfinish(session):
print("\n--- Session Synthesis Results ---")
results = get_session_synthesis_dct(session)
for test_id, res in results.items():
custom = getattr(res, 'custom_metric_value', 'N/A')
print(f"Test: {test_id}, Outcome: {res.outcome}, Custom: {custom}")
print("---------------------------------")
# test_example.py
def test_success(request, custom_metric_value):
request.node.add_pytest_stats(custom_metric_value=custom_metric_value)
assert True
def test_failure(request, custom_metric_value):
request.node.add_pytest_stats(custom_metric_value=custom_metric_value + '_failed')
pytest.fail("Intentionally failed test")
# To run these tests and see the output:
# 1. Save the conftest.py content as 'conftest.py' in your test directory.
# 2. Save the test_example.py content as 'test_example.py' in the same directory.
# 3. Run from your terminal: pytest -s