Pytest OpenTelemetry Plugin
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
- gotcha pytest-opentelemetry itself only instruments; it requires the OpenTelemetry SDK and at least one exporter (e.g., `opentelemetry-exporter-otlp`) to be installed and configured to actually send traces. Without an exporter, spans will be collected in memory but not sent anywhere.
- gotcha Configuration can be done via standard OpenTelemetry environment variables (e.g., `OTEL_SERVICE_NAME`, `OTEL_EXPORTER_OTLP_ENDPOINT`) or specific `pytest` CLI flags (e.g., `--otel-service-name`, `--otel-endpoint`). Command-line arguments always take precedence over environment variables, which can lead to unexpected behavior if both are set conflictingly.
- breaking The OpenTelemetry Python SDK, which `pytest-opentelemetry` relies upon, frequently introduces breaking changes or deprecations, especially for experimental features or during its stabilization phase for metrics and logs. Upgrading the underlying `opentelemetry-sdk` or related `opentelemetry-exporter-*` packages might introduce incompatibilities.
- gotcha The plugin has a dependency on `pytest>=7.x`. Using it with older versions of `pytest` may result in unexpected behavior or errors.
- gotcha The `pytest-opentelemetry` README (as of version 1.x) states that it defaults to OTLP over gRPC for trace export. If your OpenTelemetry Collector or backend expects HTTP/protobuf, you must explicitly configure the `OTEL_EXPORTER_OTLP_PROTOCOL` environment variable.
Install
-
pip install pytest-opentelemetry opentelemetry-sdk opentelemetry-exporter-otlp
Imports
- pytest-opentelemetry
This is a pytest plugin and automatically registers upon installation. No direct Python imports are typically needed in your test files or conftest.py to enable its core functionality.
Quickstart
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