{"id":4721,"library":"pytest-opentelemetry","title":"Pytest OpenTelemetry Plugin","description":"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.","status":"active","version":"1.1.0","language":"en","source_language":"en","source_url":"https://github.com/chrisguidry/pytest-opentelemetry","tags":["pytest","opentelemetry","tracing","observability","testing","plugin","otel"],"install":[{"cmd":"pip install pytest-opentelemetry opentelemetry-sdk opentelemetry-exporter-otlp","lang":"bash","label":"Install with OTLP exporter"}],"dependencies":[{"reason":"Core testing framework that the plugin extends.","package":"pytest","optional":false},{"reason":"OpenTelemetry Python SDK for tracing and span management.","package":"opentelemetry-sdk","optional":false},{"reason":"Required to export telemetry data using the OpenTelemetry Protocol (OTLP). Other exporters like `opentelemetry-exporter-jaeger` can also be used.","package":"opentelemetry-exporter-otlp","optional":false},{"reason":"Specific OTLP exporter for gRPC protocol, often required by default for OTLP.","package":"opentelemetry-exporter-otlp-proto-grpc","optional":true},{"reason":"Specific OTLP exporter for HTTP/protobuf protocol, if chosen.","package":"opentelemetry-exporter-otlp-proto-http","optional":true}],"imports":[{"note":"Interaction is primarily via pytest CLI flags or OpenTelemetry environment variables.","symbol":"pytest-opentelemetry","correct":"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":{"code":"import pytest\nimport os\n\n# A simple test file (e.g., test_example.py)\n\ndef test_success():\n    assert True\n\ndef test_failure():\n    assert False\n\n@pytest.mark.skip(reason=\"demonstrating skip\")\ndef test_skipped():\n    assert True\n\n# To run and see console output:\n# Set environment variables for console exporter for demonstration\n# In a real scenario, you'd point to an OpenTelemetry Collector\n# os.environ['OTEL_TRACES_EXPORTER'] = 'console'\n# os.environ['OTEL_SERVICE_NAME'] = 'my-pytest-suite'\n\n# Then run from your terminal:\n# pytest --export-traces test_example.py -s -v","lang":"python","description":"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`."},"warnings":[{"fix":"Ensure you `pip install opentelemetry-sdk opentelemetry-exporter-otlp` (or another desired exporter) alongside `pytest-opentelemetry`. Configure the exporter endpoint and protocol using standard OpenTelemetry environment variables (e.g., `OTEL_EXPORTER_OTLP_ENDPOINT`, `OTEL_EXPORTER_OTLP_PROTOCOL`) or CLI flags.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Standardize your configuration method. If using environment variables, ensure no conflicting CLI flags are used. If using CLI flags, verify they are correctly applied and override any environment variables as intended.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Always review the OpenTelemetry Python SDK changelogs when updating `opentelemetry-sdk` or related packages. Pin your `opentelemetry-*` package versions in your `requirements.txt` to ensure consistent behavior.","message":"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.","severity":"breaking","affected_versions":"Any `opentelemetry-sdk` version updates, especially across major/minor boundaries in OpenTelemetry SDK."},{"fix":"Ensure your project's `pytest` version is 7.0 or newer. Upgrade `pytest` if necessary: `pip install --upgrade pytest`.","message":"The plugin has a dependency on `pytest>=7.x`. Using it with older versions of `pytest` may result in unexpected behavior or errors.","severity":"gotcha","affected_versions":"<1.0.0 (and implied for 1.x.x)"},{"fix":"Set `OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf` if your collector expects HTTP. Also ensure the corresponding exporter package is installed (e.g., `opentelemetry-exporter-otlp-proto-http`).","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}