Pytest OpenTelemetry Plugin

1.1.0 · active · verified Sun Apr 12

pytest-opentelemetry is a pytest plugin that instruments your test runs, exporting detailed span data and timing information via OpenTelemetry. It transforms traditional pass/fail test results into rich, queryable telemetry data, allowing users to analyze test performance, identify slow or flaky tests, and visualize the full test session as a distributed trace. The library is actively maintained, currently at version 1.1.0, with frequent minor releases to add features and improve stability.

Warnings

Install

Imports

Quickstart

To quickly get started, install the plugin along with the OpenTelemetry SDK and an OTLP exporter. Create a simple test file. The plugin registers automatically. To activate tracing and export spans, run `pytest` with the `--export-traces` flag. By default, it attempts to export to an OpenTelemetry Collector at `http://localhost:4317` using OTLP gRPC. For a basic demonstration without a collector, configure the `OTEL_TRACES_EXPORTER` environment variable to `console`.

import pytest
import os

# A simple test file (e.g., test_example.py)

def test_success():
    assert True

def test_failure():
    assert False

@pytest.mark.skip(reason="demonstrating skip")
def test_skipped():
    assert True

# To run and see console output:
# Set environment variables for console exporter for demonstration
# In a real scenario, you'd point to an OpenTelemetry Collector
# os.environ['OTEL_TRACES_EXPORTER'] = 'console'
# os.environ['OTEL_SERVICE_NAME'] = 'my-pytest-suite'

# Then run from your terminal:
# pytest --export-traces test_example.py -s -v

view raw JSON →