pytest-harvest

1.10.5 · active · verified Thu Apr 16

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a custom metric using a fixture, store its value during test execution with `request.node.add_pytest_stats`, and then retrieve all collected data at the end of the session using `get_session_synthesis_dct` within a `pytest_sessionfinish` hook. Run `pytest -s` from your terminal to see the results printed.

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

view raw JSON →