11# Copyright (c) Microsoft Corporation.
22# Licensed under the MIT License.
33
4+ import os
5+ from pathlib import Path
6+ import sys
47import unittest
8+ import pytest
59from urllib .parse import urlparse
610
711from microsoft_agents_a365 .observability .core import (
@@ -35,6 +39,14 @@ class TestInvokeAgentScope(unittest.TestCase):
3539 def setUpClass (cls ):
3640 """Set up test environment once for all tests."""
3741 # Configure Microsoft Agent 365 for testing
42+ os .environ ["ENABLE_A365_OBSERVABILITY" ] = "true"
43+
44+ # Set up tracer to capture spans
45+ cls .span_exporter = InMemorySpanExporter ()
46+ tracer_provider = TracerProvider ()
47+ tracer_provider .add_span_processor (SimpleSpanProcessor (cls .span_exporter ))
48+ trace .set_tracer_provider (tracer_provider )
49+
3850 configure (
3951 service_name = "test-invoke-agent-service" ,
4052 service_namespace = "test-namespace" ,
@@ -121,12 +133,6 @@ def test_record_output_messages_method_exists(self):
121133
122134 def test_request_attributes_set_on_span (self ):
123135 """Test that request parameters from mock data are available on span attributes."""
124- # Set up tracer to capture spans
125- span_exporter = InMemorySpanExporter ()
126- tracer_provider = TracerProvider ()
127- tracer_provider .add_span_processor (SimpleSpanProcessor (span_exporter ))
128- trace .set_tracer_provider (tracer_provider )
129-
130136 # Create scope with request
131137 scope = InvokeAgentScope .start (
132138 invoke_agent_details = self .invoke_details ,
@@ -138,42 +144,42 @@ def test_request_attributes_set_on_span(self):
138144 scope .dispose ()
139145
140146 # Check if mock data parameters are available in span attributes
141- finished_spans = span_exporter .get_finished_spans ()
142-
143- if finished_spans :
144- # Get attributes from the span
145- span = finished_spans [- 1 ]
146- span_attributes = getattr (span , "attributes" , {}) or {}
147-
148- # Verify mock data request parameters are in span attributes
149- # Check source channel name from mock data
150- if GEN_AI_EXECUTION_SOURCE_NAME_KEY in span_attributes :
151- self .assertEqual (
152- span_attributes [GEN_AI_EXECUTION_SOURCE_NAME_KEY ],
153- self .source_metadata .name , # From cls.source_metadata.name
154- )
155-
156- # Check source channel description from mock data
157- if GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY in span_attributes :
158- self .assertEqual (
159- span_attributes [GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY ],
160- self .source_metadata .description , # From cls.source_metadata.description
161- )
162-
163- # Check execution type from mock data
164- if GEN_AI_EXECUTION_TYPE_KEY in span_attributes :
165- self .assertEqual (
166- span_attributes [GEN_AI_EXECUTION_TYPE_KEY ],
167- self .test_request .execution_type .value , # From cls.test_request.execution_type
168- )
169-
170- # Check input messages contain request content from mock data
171- if GEN_AI_INPUT_MESSAGES_KEY in span_attributes :
172- input_messages = span_attributes [GEN_AI_INPUT_MESSAGES_KEY ]
173- self .assertIn (
174- self .test_request .content , # From cls.test_request.content
175- input_messages ,
176- )
147+ finished_spans = self . span_exporter .get_finished_spans ()
148+ self . assertTrue ( finished_spans , "Expected at least one span to be created" )
149+
150+ # Get attributes from the span
151+ span = finished_spans [- 1 ]
152+ span_attributes = getattr (span , "attributes" , {}) or {}
153+
154+ # Verify mock data request parameters are in span attributes
155+ # Check source channel name from mock data
156+ if GEN_AI_EXECUTION_SOURCE_NAME_KEY in span_attributes :
157+ self .assertEqual (
158+ span_attributes [GEN_AI_EXECUTION_SOURCE_NAME_KEY ],
159+ self .source_metadata .name , # From cls.source_metadata.name
160+ )
161+
162+ # Check source channel description from mock data
163+ if GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY in span_attributes :
164+ self .assertEqual (
165+ span_attributes [GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY ],
166+ self .source_metadata .description , # From cls.source_metadata.description
167+ )
168+
169+ # Check execution type from mock data
170+ if GEN_AI_EXECUTION_TYPE_KEY in span_attributes :
171+ self .assertEqual (
172+ span_attributes [GEN_AI_EXECUTION_TYPE_KEY ],
173+ self .test_request .execution_type .value , # From cls.test_request.execution_type
174+ )
175+
176+ # Check input messages contain request content from mock data
177+ if GEN_AI_INPUT_MESSAGES_KEY in span_attributes :
178+ input_messages = span_attributes [GEN_AI_INPUT_MESSAGES_KEY ]
179+ self .assertIn (
180+ self .test_request .content , # From cls.test_request.content
181+ input_messages ,
182+ )
177183
178184 def test_caller_agent_client_ip_in_scope (self ):
179185 """Test that caller agent client IP is properly handled when creating InvokeAgentScope."""
@@ -209,4 +215,5 @@ def test_caller_agent_client_ip_in_scope(self):
209215
210216
211217if __name__ == "__main__" :
212- unittest .main (verbosity = 2 )
218+ # Run pytest only on the current file
219+ sys .exit (pytest .main ([str (Path (__file__ ))] + sys .argv [1 :]))
0 commit comments