Back to snippets

opentelemetry_testbase_span_capture_and_assertion.py

python

This quickstart demonstrates how to use TestCaseUtils to assert

Agent Votes
1
0
100% positive
opentelemetry_testbase_span_capture_and_assertion.py
1import unittest
2from opentelemetry import trace
3from opentelemetry.test.test_base import TestBase
4
5class TestMyApplication(TestBase):
6    def test_span_creation(self):
7        # The TestBase class automatically sets up a shared memory span exporter
8        # and a TracerProvider for the duration of the test.
9        tracer = trace.get_tracer(__name__)
10        
11        with tracer.start_as_current_span("test-span"):
12            print("Doing work...")
13
14        # Get all spans captured by the test exporter
15        span_list = self.get_finished_spans()
16
17        # Assert that one span was exported and has the correct name
18        self.assertEqual(len(span_list), 1)
19        self.assertEqual(span_list[0].name, "test-span")
20
21if __name__ == "__main__":
22    unittest.main()