From 6b509e33f6041bf11ddda12ebb744210268b1834 Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Fri, 21 Mar 2025 18:26:04 +0200 Subject: [PATCH] empty assertions --- tests/test_agent_tracing.py | 12 +++++------- tests/test_responses_tracing.py | 21 ++++++++------------- tests/test_tracing.py | 10 +++------- tests/testing_processor.py | 16 ++++++++++++++-- 4 files changed, 30 insertions(+), 29 deletions(-) diff --git a/tests/test_agent_tracing.py b/tests/test_agent_tracing.py index 8318b60..489e9b6 100644 --- a/tests/test_agent_tracing.py +++ b/tests/test_agent_tracing.py @@ -9,7 +9,7 @@ from agents import Agent, RunConfig, Runner, trace from .fake_model import FakeModel from .test_responses import get_text_message -from .testing_processor import fetch_normalized_spans, fetch_traces +from .testing_processor import assert_no_traces, fetch_normalized_spans, fetch_traces @pytest.mark.asyncio @@ -164,7 +164,7 @@ async def test_parent_disabled_trace_disabled_agent_trace(): await Runner.run(agent, input="first_test") - assert fetch_normalized_spans() == snapshot([]) + assert_no_traces() @pytest.mark.asyncio @@ -178,7 +178,7 @@ async def test_manual_disabling_works(): await Runner.run(agent, input="first_test", run_config=RunConfig(tracing_disabled=True)) - assert fetch_normalized_spans() == snapshot([]) + assert_no_traces() @pytest.mark.asyncio @@ -370,8 +370,7 @@ async def test_parent_disabled_trace_disables_streaming_agent_trace(): async for _ in x.stream_events(): pass - traces = fetch_traces() - assert len(traces) == 0, f"Expected 0 traces, got {len(traces)}" + assert_no_traces() @pytest.mark.asyncio @@ -392,5 +391,4 @@ async def test_manual_streaming_disabling_works(): async for _ in x.stream_events(): pass - traces = fetch_traces() - assert len(traces) == 0, f"Expected 0 traces, got {len(traces)}" + assert_no_traces() diff --git a/tests/test_responses_tracing.py b/tests/test_responses_tracing.py index eda65cf..95a960f 100644 --- a/tests/test_responses_tracing.py +++ b/tests/test_responses_tracing.py @@ -7,7 +7,7 @@ from agents import ModelSettings, ModelTracing, OpenAIResponsesModel, trace from agents.tracing.span_data import ResponseSpanData from tests import fake_model -from .testing_processor import fetch_normalized_spans, fetch_ordered_spans +from .testing_processor import fetch_normalized_spans, fetch_ordered_spans, assert_no_spans class DummyTracing: @@ -89,9 +89,8 @@ async def test_non_data_tracing_doesnt_set_response_id(monkeypatch): [{"workflow_name": "test", "children": [{"type": "response"}]}] ) - spans = fetch_ordered_spans() - assert len(spans) == 1 - assert spans[0].span_data.response is None + [span] = fetch_ordered_spans() + assert span.span_data.response is None @pytest.mark.allow_call_model_methods @@ -116,9 +115,7 @@ async def test_disable_tracing_does_not_create_span(monkeypatch): assert fetch_normalized_spans() == snapshot([{"workflow_name": "test"}]) - spans = fetch_ordered_spans() - assert len(spans) == 0 - + assert_no_spans() @pytest.mark.allow_call_model_methods @pytest.mark.asyncio @@ -190,10 +187,9 @@ async def test_stream_non_data_tracing_doesnt_set_response_id(monkeypatch): [{"workflow_name": "test", "children": [{"type": "response"}]}] ) - spans = fetch_ordered_spans() - assert len(spans) == 1 - assert isinstance(spans[0].span_data, ResponseSpanData) - assert spans[0].span_data.response is None + [span] = fetch_ordered_spans() + assert isinstance(span.span_data, ResponseSpanData) + assert span.span_data.response is None @pytest.mark.allow_call_model_methods @@ -226,5 +222,4 @@ async def test_stream_disabled_tracing_doesnt_create_span(monkeypatch): assert fetch_normalized_spans() == snapshot([{"workflow_name": "test"}]) - spans = fetch_ordered_spans() - assert len(spans) == 0 + assert_no_spans() \ No newline at end of file diff --git a/tests/test_tracing.py b/tests/test_tracing.py index a4520cd..773d8b9 100644 --- a/tests/test_tracing.py +++ b/tests/test_tracing.py @@ -24,6 +24,7 @@ from .testing_processor import ( fetch_normalized_spans, fetch_ordered_spans, fetch_traces, + assert_no_traces, ) ### HELPERS @@ -281,10 +282,7 @@ def disabled_tracing(): def test_disabled_tracing(): disabled_tracing() - - spans, traces = fetch_ordered_spans(), fetch_traces() - assert len(spans) == 0 - assert len(traces) == 0 + assert_no_traces() def enabled_trace_disabled_span(): @@ -374,9 +372,7 @@ async def test_noop_span_doesnt_record(): with custom_span(name="span_1") as span: span.set_error(SpanError(message="test", data={})) - spans, traces = fetch_ordered_spans(), fetch_traces() - assert len(spans) == 0 - assert len(traces) == 0 + assert_no_traces() assert t.export() is None assert span.export() is None diff --git a/tests/testing_processor.py b/tests/testing_processor.py index b07dea5..a8e0d40 100644 --- a/tests/testing_processor.py +++ b/tests/testing_processor.py @@ -80,6 +80,19 @@ def fetch_events() -> list[TestSpanProcessorEvent]: return SPAN_PROCESSOR_TESTING._events +def assert_no_spans(): + spans = fetch_ordered_spans() + if spans: + raise AssertionError(f"Expected 0 spans, got {len(spans)}") + + +def assert_no_traces(): + traces = fetch_traces() + if traces: + raise AssertionError(f"Expected 0 traces, got {len(traces)}") + assert_no_spans() + + def fetch_normalized_spans(keep_span_id: bool = False): nodes: dict[tuple[str, str | None], dict[str, Any]] = {} traces = [] @@ -92,8 +105,7 @@ def fetch_normalized_spans(keep_span_id: bool = False): nodes[(trace_obj.trace_id, None)] = trace traces.append(trace) - if not traces: - assert not fetch_ordered_spans() + assert traces, "Use assert_no_traces() to check for empty traces" for span_obj in fetch_ordered_spans(): span = span_obj.export()