Skip to content

Commit 5348bf5

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 5348bf5

File tree

1 file changed

+82
-33
lines changed

1 file changed

+82
-33
lines changed

tests/test_basics.py

Lines changed: 82 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,96 @@ 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+
@pytest.mark.forked
938+
def test_staticmethod_class_tracing(sentry_init, capture_events):
939+
sentry_init(
940+
debug=True,
941+
traces_sample_rate=1.0,
942+
functions_to_trace=[
943+
{"qualified_name": "tests.test_basics.TracingTestClass.static"}
944+
],
945+
)
940946

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"
947+
events = capture_events()
950948

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

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
952+
(event,) = events
953+
assert event["type"] == "transaction"
954+
assert event["transaction"] == "test"
957955

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

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

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"
960+
@pytest.mark.forked
961+
def test_staticmethod_instance_tracing(sentry_init, capture_events):
962+
sentry_init(
963+
debug=True,
964+
traces_sample_rate=1.0,
965+
functions_to_trace=[
966+
{"qualified_name": "tests.test_basics.TracingTestClass.static"}
967+
],
968+
)
969+
970+
events = capture_events()
971+
972+
with sentry_sdk.start_transaction(name="test"):
973+
assert TracingTestClass().static(1) == 1
974+
975+
(event,) = events
976+
assert event["type"] == "transaction"
977+
assert event["transaction"] == "test"
971978

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

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
1025+
(span,) = event["spans"]
1026+
assert span["description"] == "tests.test_basics.TracingTestClass.class_"
9781027

9791028

9801029
def test_last_event_id(sentry_init):

0 commit comments

Comments
 (0)