33
44import pytest
55
6+ import sentry_sdk
67from sentry_sdk .tracing import trace
78from sentry_sdk .tracing_utils import start_child_span_decorator
89from sentry_sdk .utils import logger
9- from tests .conftest import patch_start_tracing_child
1010
1111
1212def my_example_function ():
@@ -17,68 +17,80 @@ async def my_async_example_function():
1717 return "return_of_async_function"
1818
1919
20- @pytest .mark .forked
21- def test_trace_decorator ():
22- with patch_start_tracing_child () as fake_start_child :
20+ def test_trace_decorator (sentry_init , capture_events ):
21+ sentry_init (traces_sample_rate = 1.0 )
22+ events = capture_events ()
23+
24+ with sentry_sdk .start_span (name = "test" ):
2325 result = my_example_function ()
24- fake_start_child .assert_not_called ()
2526 assert result == "return_of_sync_function"
2627
27- result2 = start_child_span_decorator (my_example_function )()
28- fake_start_child .assert_called_once_with (
29- op = "function" , name = "test_decorator.my_example_function"
30- )
28+ result2 = trace (my_example_function )()
3129 assert result2 == "return_of_sync_function"
3230
31+ (event ,) = events
32+ (span ,) = event ["spans" ]
33+ assert span ["op" ] == "function"
34+ assert span ["description" ] == "test_decorator.my_example_function"
3335
34- @pytest .mark .forked
35- def test_trace_decorator_no_trx ():
36- with patch_start_tracing_child (fake_transaction_is_none = True ):
37- with mock .patch .object (logger , "debug" , mock .Mock ()) as fake_debug :
38- result = my_example_function ()
39- fake_debug .assert_not_called ()
40- assert result == "return_of_sync_function"
4136
42- result2 = start_child_span_decorator (my_example_function )()
43- fake_debug .assert_called_once_with (
44- "Cannot create a child span for %s. "
45- "Please start a Sentry transaction before calling this function." ,
46- "test_decorator.my_example_function" ,
47- )
48- assert result2 == "return_of_sync_function"
37+ def test_trace_decorator_no_trx (sentry_init , capture_events ):
38+ sentry_init (traces_sample_rate = 1.0 )
39+ events = capture_events ()
40+
41+ with mock .patch .object (logger , "debug" , mock .Mock ()) as fake_debug :
42+ result = my_example_function ()
43+ assert result == "return_of_sync_function"
44+ fake_debug .assert_not_called ()
45+
46+ result2 = trace (my_example_function )()
47+ assert result2 == "return_of_sync_function"
48+ fake_debug .assert_called_once_with (
49+ "Cannot create a child span for %s. "
50+ "Please start a Sentry transaction before calling this function." ,
51+ "test_decorator.my_example_function" ,
52+ )
53+
54+ assert len (events ) == 0
4955
5056
51- @pytest .mark .forked
5257@pytest .mark .asyncio
53- async def test_trace_decorator_async ():
54- with patch_start_tracing_child () as fake_start_child :
58+ async def test_trace_decorator_async (sentry_init , capture_events ):
59+ sentry_init (traces_sample_rate = 1.0 )
60+ events = capture_events ()
61+
62+ with sentry_sdk .start_span (name = "test" ):
5563 result = await my_async_example_function ()
56- fake_start_child .assert_not_called ()
5764 assert result == "return_of_async_function"
5865
59- result2 = await start_child_span_decorator (my_async_example_function )()
60- fake_start_child .assert_called_once_with (
61- op = "function" ,
62- name = "test_decorator.my_async_example_function" ,
63- )
66+ result2 = await trace (my_async_example_function )()
6467 assert result2 == "return_of_async_function"
6568
69+ (event ,) = events
70+ (span ,) = event ["spans" ]
71+ assert span ["op" ] == "function"
72+ assert span ["description" ] == "test_decorator.my_async_example_function"
73+
6674
6775@pytest .mark .asyncio
68- async def test_trace_decorator_async_no_trx ():
69- with patch_start_tracing_child (fake_transaction_is_none = True ):
70- with mock .patch .object (logger , "debug" , mock .Mock ()) as fake_debug :
71- result = await my_async_example_function ()
72- fake_debug .assert_not_called ()
73- assert result == "return_of_async_function"
74-
75- result2 = await start_child_span_decorator (my_async_example_function )()
76- fake_debug .assert_called_once_with (
77- "Cannot create a child span for %s. "
78- "Please start a Sentry transaction before calling this function." ,
79- "test_decorator.my_async_example_function" ,
80- )
81- assert result2 == "return_of_async_function"
76+ async def test_trace_decorator_async_no_trx (sentry_init , capture_events ):
77+ sentry_init (traces_sample_rate = 1.0 )
78+ events = capture_events ()
79+
80+ with mock .patch .object (logger , "debug" , mock .Mock ()) as fake_debug :
81+ result = await my_async_example_function ()
82+ fake_debug .assert_not_called ()
83+ assert result == "return_of_async_function"
84+
85+ result2 = await start_child_span_decorator (my_async_example_function )()
86+ fake_debug .assert_called_once_with (
87+ "Cannot create a child span for %s. "
88+ "Please start a Sentry transaction before calling this function." ,
89+ "test_decorator.my_async_example_function" ,
90+ )
91+ assert result2 == "return_of_async_function"
92+
93+ assert len (events ) == 0
8294
8395
8496def test_functions_to_trace_signature_unchanged_sync (sentry_init ):
0 commit comments