Skip to content

Commit 742bd9f

Browse files
test(tracing): Simplify static/classmethod tracing tests
These tests were causing flakes where the mock method was being called more than once. The tests were also difficult to understand. This change removes the need for mocking (hopefully increasing test stability) and also should hopefully make it easier to understand what these tests are meant to be checking
1 parent 6000f87 commit 742bd9f

File tree

1 file changed

+78
-33
lines changed

1 file changed

+78
-33
lines changed

tests/test_basics.py

Lines changed: 78 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import pytest
1010
from sentry_sdk.client import Client
1111
from sentry_sdk.utils import datetime_from_isoformat
12-
from tests.conftest import patch_start_tracing_child
1312

1413
import sentry_sdk
1514
import sentry_sdk.scope
@@ -935,46 +934,92 @@ def class_(cls, arg):
935934
return cls, arg
936935

937936

938-
def test_staticmethod_tracing(sentry_init):
939-
test_staticmethod_name = "tests.test_basics.TracingTestClass.static"
937+
def test_staticmethod_class_tracing(sentry_init, capture_events):
938+
sentry_init(
939+
debug=True,
940+
traces_sample_rate=1.0,
941+
functions_to_trace=[
942+
{"qualified_name": "tests.test_basics.TracingTestClass.static"}
943+
],
944+
)
940945

941-
assert (
942-
".".join(
943-
[
944-
TracingTestClass.static.__module__,
945-
TracingTestClass.static.__qualname__,
946-
]
947-
)
948-
== test_staticmethod_name
949-
), "The test static method was moved or renamed. Please update the name accordingly"
946+
events = capture_events()
950947

951-
sentry_init(functions_to_trace=[{"qualified_name": test_staticmethod_name}])
948+
with sentry_sdk.start_transaction(name="test"):
949+
assert TracingTestClass.static(1) == 1
952950

953-
for instance_or_class in (TracingTestClass, TracingTestClass()):
954-
with patch_start_tracing_child() as fake_start_child:
955-
assert instance_or_class.static(1) == 1
956-
assert fake_start_child.call_count == 1
951+
(event,) = events
952+
assert event["type"] == "transaction"
953+
assert event["transaction"] == "test"
957954

955+
(span,) = event["spans"]
956+
assert span["description"] == "tests.test_basics.TracingTestClass.static"
958957

959-
def test_classmethod_tracing(sentry_init):
960-
test_classmethod_name = "tests.test_basics.TracingTestClass.class_"
961958

962-
assert (
963-
".".join(
964-
[
965-
TracingTestClass.class_.__module__,
966-
TracingTestClass.class_.__qualname__,
967-
]
968-
)
969-
== test_classmethod_name
970-
), "The test class method was moved or renamed. Please update the name accordingly"
959+
def test_staticmethod_instance_tracing(sentry_init, capture_events):
960+
sentry_init(
961+
debug=True,
962+
traces_sample_rate=1.0,
963+
functions_to_trace=[
964+
{"qualified_name": "tests.test_basics.TracingTestClass.static"}
965+
],
966+
)
967+
968+
events = capture_events()
969+
970+
with sentry_sdk.start_transaction(name="test"):
971+
assert TracingTestClass().static(1) == 1
972+
973+
(event,) = events
974+
assert event["type"] == "transaction"
975+
assert event["transaction"] == "test"
971976

972-
sentry_init(functions_to_trace=[{"qualified_name": test_classmethod_name}])
977+
(span,) = event["spans"]
978+
assert span["description"] == "tests.test_basics.TracingTestClass.static"
979+
980+
981+
def test_classmethod_class_tracing(sentry_init, capture_events):
982+
sentry_init(
983+
debug=True,
984+
traces_sample_rate=1.0,
985+
functions_to_trace=[
986+
{"qualified_name": "tests.test_basics.TracingTestClass.class_"}
987+
],
988+
)
989+
990+
events = capture_events()
991+
992+
with sentry_sdk.start_transaction(name="test"):
993+
assert TracingTestClass.class_(1) == (TracingTestClass, 1)
994+
995+
(event,) = events
996+
assert event["type"] == "transaction"
997+
assert event["transaction"] == "test"
998+
999+
(span,) = event["spans"]
1000+
assert span["description"] == "tests.test_basics.TracingTestClass.class_"
1001+
1002+
1003+
def test_classmethod_instance_tracing(sentry_init, capture_events):
1004+
sentry_init(
1005+
debug=True,
1006+
traces_sample_rate=1.0,
1007+
functions_to_trace=[
1008+
{"qualified_name": "tests.test_basics.TracingTestClass.class_"}
1009+
],
1010+
)
1011+
1012+
events = capture_events()
1013+
1014+
with sentry_sdk.start_transaction(name="test"):
1015+
assert TracingTestClass().class_(1) == (TracingTestClass, 1)
1016+
1017+
(event,) = events
1018+
assert event["type"] == "transaction"
1019+
assert event["transaction"] == "test"
9731020

974-
for instance_or_class in (TracingTestClass, TracingTestClass()):
975-
with patch_start_tracing_child() as fake_start_child:
976-
assert instance_or_class.class_(1) == (TracingTestClass, 1)
977-
assert fake_start_child.call_count == 1
1021+
(span,) = event["spans"]
1022+
assert span["description"] == "tests.test_basics.TracingTestClass.class_"
9781023

9791024

9801025
def test_last_event_id(sentry_init):

0 commit comments

Comments
 (0)