Skip to content

Commit 0f0eddb

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 0f0eddb

File tree

1 file changed

+86
-33
lines changed

1 file changed

+86
-33
lines changed

tests/test_basics.py

Lines changed: 86 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,100 @@ 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+
# We need to fork here because the test modifies tests.test_basics.TracingTestClass
938+
@pytest.mark.forked
939+
def test_staticmethod_class_tracing(sentry_init, capture_events):
940+
sentry_init(
941+
debug=True,
942+
traces_sample_rate=1.0,
943+
functions_to_trace=[
944+
{"qualified_name": "tests.test_basics.TracingTestClass.static"}
945+
],
946+
)
940947

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"
948+
events = capture_events()
950949

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

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

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

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

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

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

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

9791032

9801033
def test_last_event_id(sentry_init):

0 commit comments

Comments
 (0)