Skip to content

Commit 5f8e109

Browse files
committed
Fixes for tests.
1 parent 63ee9b7 commit 5f8e109

4 files changed

Lines changed: 125 additions & 85 deletions

File tree

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/execute_tool_scope.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def start(
4242
Returns:
4343
A new ExecuteToolScope instance
4444
"""
45-
return ExecuteToolScope(details, agent_details, tenant_details)
45+
return ExecuteToolScope(details, agent_details, tenant_details, request)
4646

4747
def __init__(
4848
self,

tests/observability/core/test_execute_tool_scope.py

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
import os
5+
from pathlib import Path
6+
import sys
47
import unittest
8+
import pytest
59

610
from microsoft_agents_a365.observability.core import (
711
AgentDetails,
812
ExecutionType,
913
ExecuteToolScope,
14+
OpenTelemetryScope,
1015
Request,
1116
SourceMetadata,
1217
TenantDetails,
@@ -30,6 +35,14 @@ class TestExecuteToolScope(unittest.TestCase):
3035
def setUpClass(cls):
3136
"""Set up test environment once for all tests."""
3237
# Configure Microsoft Agent 365 for testing
38+
os.environ["ENABLE_A365_OBSERVABILITY"] = "true"
39+
40+
# Set up tracer to capture spans
41+
cls.span_exporter = InMemorySpanExporter()
42+
tracer_provider = TracerProvider()
43+
tracer_provider.add_span_processor(SimpleSpanProcessor(cls.span_exporter))
44+
trace.set_tracer_provider(tracer_provider)
45+
3346
configure(
3447
service_name="test-execute-tool-service",
3548
service_namespace="test-namespace",
@@ -60,11 +73,6 @@ def test_record_response_method_exists(self):
6073

6174
def test_request_metadata_set_on_span(self):
6275
"""Test that request source metadata is set on span attributes."""
63-
span_exporter = InMemorySpanExporter()
64-
tracer_provider = TracerProvider()
65-
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter))
66-
trace.set_tracer_provider(tracer_provider)
67-
6876
request = Request(
6977
content="Execute tool with request metadata",
7078
execution_type=ExecutionType.AGENT_TO_AGENT,
@@ -79,24 +87,33 @@ def test_request_metadata_set_on_span(self):
7987
if scope is not None:
8088
scope.dispose()
8189

82-
finished_spans = span_exporter.get_finished_spans()
90+
finished_spans = self.span_exporter.get_finished_spans()
91+
self.assertTrue(finished_spans, "Expected at least one span to be created")
8392

84-
if finished_spans:
85-
span = finished_spans[-1]
86-
span_attributes = getattr(span, "attributes", {}) or {}
93+
span = finished_spans[-1]
94+
span_attributes = getattr(span, "attributes", {}) or {}
8795

88-
if GEN_AI_EXECUTION_SOURCE_NAME_KEY in span_attributes:
89-
self.assertEqual(
90-
span_attributes[GEN_AI_EXECUTION_SOURCE_NAME_KEY],
91-
request.source_metadata.name,
92-
)
96+
self.assertIn(
97+
GEN_AI_EXECUTION_SOURCE_NAME_KEY,
98+
span_attributes,
99+
"Expected source name to be set on span",
100+
)
101+
self.assertEqual(
102+
span_attributes[GEN_AI_EXECUTION_SOURCE_NAME_KEY],
103+
request.source_metadata.name,
104+
)
93105

94-
if GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY in span_attributes:
95-
self.assertEqual(
96-
span_attributes[GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY],
97-
request.source_metadata.description,
98-
)
106+
self.assertIn(
107+
GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY,
108+
span_attributes,
109+
"Expected source description to be set on span",
110+
)
111+
self.assertEqual(
112+
span_attributes[GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY],
113+
request.source_metadata.description,
114+
)
99115

100116

101117
if __name__ == "__main__":
102-
unittest.main(verbosity=2)
118+
# Run pytest only on the current file
119+
sys.exit(pytest.main([str(Path(__file__))] + sys.argv[1:]))

tests/observability/core/test_inference_scope.py

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
import os
5+
from pathlib import Path
6+
import sys
47
import unittest
8+
import pytest
59

610
from microsoft_agents_a365.observability.core import (
711
ExecutionType,
812
InferenceCallDetails,
913
InferenceOperationType,
1014
InferenceScope,
15+
OpenTelemetryScope,
1116
Request,
1217
SourceMetadata,
1318
TenantDetails,
@@ -31,6 +36,14 @@ class TestInferenceScope(unittest.TestCase):
3136
def setUpClass(cls):
3237
"""Set up test environment once for all tests."""
3338
# Configure Microsoft Agent 365 for testing
39+
os.environ["ENABLE_A365_OBSERVABILITY"] = "true"
40+
41+
# Set up tracer to capture spans
42+
cls.span_exporter = InMemorySpanExporter()
43+
tracer_provider = TracerProvider()
44+
tracer_provider.add_span_processor(SimpleSpanProcessor(cls.span_exporter))
45+
trace.set_tracer_provider(tracer_provider)
46+
3447
configure(
3548
service_name="test-inference-service",
3649
service_namespace="test-namespace",
@@ -135,33 +148,36 @@ def test_request_metadata_set_on_span(self):
135148
source_metadata=SourceMetadata(name="Channel 1", description="Link to channel"),
136149
)
137150

138-
span_exporter = InMemorySpanExporter()
139-
tracer_provider = TracerProvider()
140-
tracer_provider.add_span_processor(SimpleSpanProcessor(span_exporter))
141-
trace.set_tracer_provider(tracer_provider)
142-
143151
scope = InferenceScope.start(details, self.agent_details, self.tenant_details, request)
144152

145153
if scope is not None:
146154
scope.dispose()
147155

148-
finished_spans = span_exporter.get_finished_spans()
156+
finished_spans = self.span_exporter.get_finished_spans()
157+
self.assertTrue(finished_spans, "Expected at least one span to be created")
149158

150-
if finished_spans:
151-
span = finished_spans[-1]
152-
span_attributes = getattr(span, "attributes", {}) or {}
159+
span = finished_spans[-1]
160+
span_attributes = getattr(span, "attributes", {}) or {}
153161

154-
if GEN_AI_EXECUTION_SOURCE_NAME_KEY in span_attributes:
155-
self.assertEqual(
156-
span_attributes[GEN_AI_EXECUTION_SOURCE_NAME_KEY],
157-
request.source_metadata.name,
158-
)
162+
self.assertIn(
163+
GEN_AI_EXECUTION_SOURCE_NAME_KEY,
164+
span_attributes,
165+
"Expected source name to be set on span",
166+
)
167+
self.assertEqual(
168+
span_attributes[GEN_AI_EXECUTION_SOURCE_NAME_KEY],
169+
request.source_metadata.name,
170+
)
159171

160-
if GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY in span_attributes:
161-
self.assertEqual(
162-
span_attributes[GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY],
163-
request.source_metadata.description,
164-
)
172+
self.assertIn(
173+
GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY,
174+
span_attributes,
175+
"Expected source description to be set on span",
176+
)
177+
self.assertEqual(
178+
span_attributes[GEN_AI_EXECUTION_SOURCE_DESCRIPTION_KEY],
179+
request.source_metadata.description,
180+
)
165181

166182
def test_inference_scope_context_manager(self):
167183
"""Test InferenceScope as context manager."""
@@ -308,5 +324,5 @@ def test_record_thought_process(self):
308324

309325

310326
if __name__ == "__main__":
311-
# Run the tests
312-
unittest.main(verbosity=2)
327+
# Run pytest only on the current file
328+
sys.exit(pytest.main([str(Path(__file__))] + sys.argv[1:]))

tests/observability/core/test_invoke_agent_scope.py

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Copyright (c) Microsoft Corporation.
22
# Licensed under the MIT License.
33

4+
import os
5+
from pathlib import Path
6+
import sys
47
import unittest
8+
import pytest
59
from urllib.parse import urlparse
610

711
from 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

211217
if __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

Comments
 (0)