Package: pytest-opentelemetry
pytest-opentelemetryInstruments your pytest runs, exporting the spans and timing via OpenTelemetry.Why instrument my test suite?As projects grow larger, perhaps with many contributors, test suite runtime can be
a significant limiting factor to how fast you and your team can deliver changes.  By
measuring your test suite's runtime in detail, and keeping a history of this runtime
in a visualization tool likeJaeger, you can spot
test bottlenecks that might be slowing your entire suite down.Additionally,pytestmakes an excellent driver forintegrationtests that operate
on fully deployed systems, like your testing/staging environment.  By usingpytest-opentelemetryand configuring the appropriate propagators, you can connect
traces from your integration test suite to your running system to analyze failures
more quickly.Even if you only enablepytest-opentelemetrylocally for occasional debugging, it
can help you understandexactlywhat is slowing your test suite down.  Did you
forget to mock thatrequestscall?  Didn't realize the test suite was creating
10,000 example accounts?  Should that database setup fixture be markedscope=module? These are the kinds of questionspytest-opentelemetrycan help
you answer.pytest-opentelemetryworks even better when testing applications and libraries that
are themselves instrumented with OpenTelemetry.  This will give you deeper visibility
into the layers of your stack, like database queries and network requests.Installation and usagepipinstallpytest-opentelemetryInstalling a library that exposes a specific pytest-related entry point is automatically
loaded as a pytest plugin.  Simply installing the plugin should be enough to register
it for pytest.Using the--export-tracesflag enables trace exporting (otherwise, the created spans
will only be tracked in memory):pytest--export-tracesBy default, this exports traces tohttp://localhost:4317, which will work well if
you're running a localOpenTelemetry
Collectorexposing the OTLP gRPC interface.
You can use any of theOpenTelemetry environment
variablesto adjust the tracing export or behavior:exportOTEL_EXPORTER_OTLP_ENDPOINT=http://another.collector:4317
pytest--export-tracesOnly the OTLP over gRPC exporter is currently supported.pytest-opentelemetrywill use the name of the project's directory as the OpenTelemetryservice.name, but it will also respect the standardOTEL_SERVICE_NAMEandOTEL_RESOURCE_ATTRIBUTESenvironment variables.  If you would like to permanently
specify those for your project, consider using the very helpfulpytest-envpackage to set these for all test
runs, for example, in yourpyproject.toml:[tool.pytest.ini_options]env=["OTEL_RESOURCE_ATTRIBUTES=service.name=my-project",]If you are using the delightfulpytest-xdistpackage to spread your tests out over multiple processes or hosts,pytest-opentelemetrywill automatically unite them all under one trace.  If thispytestrun is part of a larger trace, you can provide a--trace-parentargument to
nest this run under that parent:pytest...--trace-parent00-1234567890abcdef1234567890abcdef-fedcba0987654321-01Visualizing test tracesOne quick way to visualize test traces would be to use anOpenTelemetry
Collectorfeeding traces toJaeger.  This can be configured with a minimal Docker
Compose file like:version:"3.8"services:jaeger:image:jaegertracing/all-in-one:1.33ports:-16686:16686# frontend-14250:14250# model.protocollector:image:otel/opentelemetry-collector-contrib:0.49.0depends_on:-jaegerports:-4317:4317# OTLP (gRPC)volumes:-./otelcol-config.yaml:/etc/otelcol-contrib/config.yaml:roWith thisotelcol-config.yaml:receivers:otlp:protocols:grpc:processors:batch:exporters:jaeger:endpoint:jaeger:14250tls:insecure:trueservice:pipelines:traces:receivers:[otlp]processors:[batch]exporters:[jaeger]DevelopingTwo references I keep returning to is the pytest guide on writing plugins, and the
pytest API reference:https://docs.pytest.org/en/6.2.x/writing_plugins.htmlhttps://docs.pytest.org/en/6.2.x/reference.html#hooksThese are extremely helpful in understanding the lifecycle of a pytest run.To get setup for development, you will likely want to use a "virtual environment", using
great tools likevirtualenvorpyenv.Once you have a virtual environment, install this package for editing, along with its
development dependencies, with this command:pipinstall-e'.[dev]'When sending pull requests, don't forget to bump the version insetup.cfg.