Skip to main content

pytest_otel

Features

pytest-otel plugin for reporting OpenTelemetry spans of tests executed.

OpenTelemetry

Requirements

  • opentelemetry-api
  • opentelemetry-exporter-otlp
  • opentelemetry-sdk
  • pytest

Installation

You can install "pytest-otel" via pip or using the setup.py script.

pip install pytest-otel

Usage

pytest_otel is configured by adding some parameters to the pytest command line. Below are the descriptions:

  • --otel-endpoint: URL for the OpenTelemetry server. (Required). Env variable: OTEL_EXPORTER_OTLP_ENDPOINT
  • --otel-headers: Additional headers to send (i.e.: key1=value1,key2=value2). Env variable: OTEL_EXPORTER_OTLP_HEADERS
  • --otel-service-name: Name of the service. Env variable: OTEL_SERVICE_NAME
  • --otel-session-name: Name for the main span.
  • --otel-traceparent: Trace parent ID. Env variable: TRACEPARENT. See https://www.w3.org/TR/trace-context-1/#trace-context-http-headers-format
  • --otel-insecure: Disables TLS. Env variable: OTEL_EXPORTER_OTLP_INSECURE
  • --otel-exporter-protocol: OTLP exporter protocol to use: 'grpc' or 'http/protobuf'. Default is 'grpc'. Env variable: OTEL_EXPORTER_OTLP_PROTOCOL
  • --otel-dotenv-path: Path to a dotenv file to load environment variables from.
pytest --otel-endpoint https://otelcollector.example.com:4317 \
       --otel-headers "authorization=Bearer ASWDCcCRFfr" \
       --otel-service-name pytest_otel \
       --otel-session-name='My_Test_cases' \
       --otel-traceparent=00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01 \
       --otel-insecure=False \
       --otel-exporter-protocol=grpc

IMPORTANT: If you use --otel-headers the transaction metadata might expose those arguments with their values. In order to avoid any credentials to be exposed, it's recommended to use the environment variables. For instance, given the above example, a similar one with environment variables can be seen below:

OTEL_EXPORTER_OTLP_ENDPOINT=https://apm.example.com:8200 \
OTEL_EXPORTER_OTLP_HEADERS="authorization=Bearer ASWDCcCRFfr" \
OTEL_SERVICE_NAME=pytest_otel \
TRACEPARENT=00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01 \
OTEL_EXPORTER_OTLP_INSECURE=False \
OTEL_EXPORTER_OTLP_PROTOCOL=grpc \
pytest --otel-session-name='My_Test_cases'

Using a Dotenv File

To avoid setting environment variables manually, you can use a dotenv file with the --otel-dotenv-path option:

# Install with dotenv support
pip install pytest-otel[dotenv]

# Create a dotenv file (e.g., otel.env)
cat > otel.env << EOF
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_RESOURCE_ATTRIBUTES=service.version=1.0.0,deployment.environment=test
OTEL_METRIC_EXPORT_INTERVAL=100
OTEL_BSP_SCHEDULE_DELAY=100
EOF

# Run tests with dotenv file
pytest --otel-dotenv-path=otel.env --otel-service-name=my_service --otel-session-name='My_Test_cases'

Note: CLI options (including defaults) always take precedence over dotenv and environment variables. The dotenv file is useful for:

  • Setting OpenTelemetry SDK environment variables like OTEL_RESOURCE_ATTRIBUTES, OTEL_METRIC_EXPORT_INTERVAL, OTEL_BSP_SCHEDULE_DELAY
  • Setting OTEL_EXPORTER_OTLP_ENDPOINT when not using the --otel-endpoint flag
  • Other OpenTelemetry configuration that the plugin doesn't explicitly manage via CLI flags

For CLI-managed options like --otel-service-name and --otel-exporter-protocol, you must pass them explicitly on the command line if you want to override the defaults. 3. Default values

To use the HTTP exporter instead of gRPC:

# Note: Using port 4318 (standard OTLP HTTP port) instead of 4317 (standard OTLP gRPC port)
pytest --otel-endpoint https://otelcollector.example.com:4318 \
       --otel-service-name pytest_otel \
       --otel-session-name='My_Test_cases' \
       --otel-exporter-protocol=http/protobuf

Demos

License

Distributed under the terms of the Apache License Version 2.0_ license, "pytest-otel" is free and open source software

Metadata

Release files for pytest-otel 2.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-otel 2.6.0
File Size Uploaded Provenance Actions
pytest_otel-2.6.0.tar.gz 21.3 kB Without provenance Details for pytest_otel-2.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-otel 2.6.0
File Interpreter ABI Platform Actions
pytest_otel-2.6.0-py2.py3-none-any.whl Python 3, Python 2 none any Details for pytest_otel-2.6.0-py2.py3-none-any.whl

Total release size: 37.2 kB

Release files / pytest_otel-2.6.0.tar.gz

Download URL pytest_otel-2.6.0.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
eaca9cc63d34ff22f233461e7ea1694ea335872f90788056eb96c3713301e194
BLAKE2b-256 checksum
How to use checksums
4019670fb15f898f9762cd6f63b9ad3886f82e4a7579332337394d3a8d86d977
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.17

Release files / pytest_otel-2.6.0-py2.py3-none-any.whl

Download URL pytest_otel-2.6.0-py2.py3-none-any.whl
Size 15.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
adb249f4f3ce4e82c8e15cbaef3550161b1809bc68d1cc0bff26e0a3ef58f30e
BLAKE2b-256 checksum
How to use checksums
7e541056023df1dea701a0e10630e4296e29706434fc6bc7f647f3c134b7b645
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.17

Security for pytest-otel 2.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

2.6.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page