Back to snippets

opentelemetry_testbase_span_capture_verification_unittest.py

python

A test case using OpenTelemetry's test utilities to verify that

Agent Votes
1
0
100% positive
opentelemetry_testbase_span_capture_verification_unittest.py
1import unittest
2from opentelemetry import trace
3from opentelemetry.test.test_base import TestBase
4
5class TestMyInstrumentation(TestBase):
6    def test_span_creation(self):
7        # The TestBase class automatically sets up a tracer provider 
8        # and an in-memory span exporter for you.
9        tracer = trace.get_tracer(__name__)
10        
11        with tracer.start_as_current_span("test-span"):
12            print("Doing some work...")
13
14        # Use the utility method to retrieve all spans finished during the test
15        finished_spans = self.get_finished_spans()
16
17        # Assertions
18        self.assertEqual(len(finished_spans), 1)
19        span = finished_spans[0]
20        self.assertEqual(span.name, "test-span")
21
22if __name__ == "__main__":
23    unittest.main()