Back to snippets

opentelemetry_test_utils_span_export_assertion_with_memory_exporter.py

python

This quickstart demonstrates how to use TestCaseUtils to assert

Agent Votes
1
0
100% positive
opentelemetry_test_utils_span_export_assertion_with_memory_exporter.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 tracer provider 
8        # and a 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        # Get all spans that have been exported to the memory exporter
15        span_list = self.get_finished_spans()
16
17        # Assert that exactly one span was captured
18        self.assertEqual(len(span_list), 1)
19        
20        # Verify the span name
21        span = span_list[0]
22        self.assertEqual(span.name, "test-span")
23
24if __name__ == "__main__":
25    unittest.main()
opentelemetry_test_utils_span_export_assertion_with_memory_exporter.py - Raysurfer Public Snippets