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.5.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.5.0
File Size Uploaded
pytest_otel-2.5.0.tar.gz 21.3 kB Details

Built distribution (wheel)

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

Total release size: 37.2 kB

Release files / pytest_otel-2.5.0.tar.gz

Download URL pytest_otel-2.5.0.tar.gz
Size 21.3 kB
Tags Source
SHA-256 checksum
How to use checksums
36a7f046644e7d67722aea85304ade4cf38300712f2dfba4479b49d2847204d7
BLAKE2b-256 checksum
How to use checksums
c5cb5123f3b4057c6c94ce8c7e75f20192f7c1125a3ebf048a0dd3e7fc29e35d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

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

Download URL pytest_otel-2.5.0-py2.py3-none-any.whl
Size 15.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5c6c5eec7fc31fd23e66a2d3669b675df61b2dc795397a3ce82337f63ec37372
BLAKE2b-256 checksum
How to use checksums
84b0cdc43afda73cf412748984e4bfd07cef31f0d2de1ace739954dc3380be4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.15

Release history Release notifications | RSS feed

2.5.1

2 release files

This release

2.5.0 This release

2 release files

2.4.0

2 release files

2.3.0

2 release files

2.2.2

2 release files

2.2.1

2 release files

2.2.0

2 release files

2.1.0

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.7.1

2 release files

1.7.0

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.3

2 release files

1.4.2

2 release files

1.4.1

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.1

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.3

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

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