Skip to content

Commit ff4f8e6

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 dfad14f commit ff4f8e6

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
@@ -7,7 +7,6 @@
77
from datetime import datetime, timedelta, timezone
88

99
import pytest
10-
from tests.conftest import patch_start_tracing_child
1110

1211
import sentry_sdk
1312
import sentry_sdk.scope
@@ -769,46 +768,100 @@ def class_(cls, arg):
769768
return cls, arg
770769

771770

772-
def test_staticmethod_tracing(sentry_init):
773-
test_staticmethod_name = "tests.test_basics.TracingTestClass.static"
771+
# We need to fork here because the test modifies tests.test_basics.TracingTestClass
772+
@pytest.mark.forked
773+
def test_staticmethod_class_tracing(sentry_init, capture_events):
774+
sentry_init(
775+
debug=True,
776+
traces_sample_rate=1.0,
777+
functions_to_trace=[
778+
{"qualified_name": "tests.test_basics.TracingTestClass.static"}
779+
],
780+
)
774781

775-
assert (
776-
".".join(
777-
[
778-
TracingTestClass.static.__module__,
779-
TracingTestClass.static.__qualname__,
780-
]
781-
)
782-
== test_staticmethod_name
783-
), "The test static method was moved or renamed. Please update the name accordingly"
782+
events = capture_events()
784783

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

787-
for instance_or_class in (TracingTestClass, TracingTestClass()):
788-
with patch_start_tracing_child() as fake_start_child:
789-
assert instance_or_class.static(1) == 1
790-
assert fake_start_child.call_count == 1
787+
(event,) = events
788+
assert event["type"] == "transaction"
789+
assert event["transaction"] == "test"
791790

791+
(span,) = event["spans"]
792+
assert span["description"] == "tests.test_basics.TracingTestClass.static"
792793

793-
def test_classmethod_tracing(sentry_init):
794-
test_classmethod_name = "tests.test_basics.TracingTestClass.class_"
795794

796-
assert (
797-
".".join(
798-
[
799-
TracingTestClass.class_.__module__,
800-
TracingTestClass.class_.__qualname__,
801-
]
802-
)
803-
== test_classmethod_name
804-
), "The test class method was moved or renamed. Please update the name accordingly"
795+
# We need to fork here because the test modifies tests.test_basics.TracingTestClass
796+
@pytest.mark.forked
797+
def test_staticmethod_instance_tracing(sentry_init, capture_events):
798+
sentry_init(
799+
debug=True,
800+
traces_sample_rate=1.0,
801+
functions_to_trace=[
802+
{"qualified_name": "tests.test_basics.TracingTestClass.static"}
803+
],
804+
)
805+
806+
events = capture_events()
807+
808+
with sentry_sdk.start_transaction(name="test"):
809+
assert TracingTestClass().static(1) == 1
810+
811+
(event,) = events
812+
assert event["type"] == "transaction"
813+
assert event["transaction"] == "test"
805814

806-
sentry_init(functions_to_trace=[{"qualified_name": test_classmethod_name}])
815+
(span,) = event["spans"]
816+
assert span["description"] == "tests.test_basics.TracingTestClass.static"
817+
818+
819+
# We need to fork here because the test modifies tests.test_basics.TracingTestClass
820+
@pytest.mark.forked
821+
def test_classmethod_class_tracing(sentry_init, capture_events):
822+
sentry_init(
823+
debug=True,
824+
traces_sample_rate=1.0,
825+
functions_to_trace=[
826+
{"qualified_name": "tests.test_basics.TracingTestClass.class_"}
827+
],
828+
)
829+
830+
events = capture_events()
831+
832+
with sentry_sdk.start_transaction(name="test"):
833+
assert TracingTestClass.class_(1) == (TracingTestClass, 1)
834+
835+
(event,) = events
836+
assert event["type"] == "transaction"
837+
assert event["transaction"] == "test"
838+
839+
(span,) = event["spans"]
840+
assert span["description"] == "tests.test_basics.TracingTestClass.class_"
841+
842+
843+
# We need to fork here because the test modifies tests.test_basics.TracingTestClass
844+
@pytest.mark.forked
845+
def test_classmethod_instance_tracing(sentry_init, capture_events):
846+
sentry_init(
847+
debug=True,
848+
traces_sample_rate=1.0,
849+
functions_to_trace=[
850+
{"qualified_name": "tests.test_basics.TracingTestClass.class_"}
851+
],
852+
)
853+
854+
events = capture_events()
855+
856+
with sentry_sdk.start_transaction(name="test"):
857+
assert TracingTestClass().class_(1) == (TracingTestClass, 1)
858+
859+
(event,) = events
860+
assert event["type"] == "transaction"
861+
assert event["transaction"] == "test"
807862

808-
for instance_or_class in (TracingTestClass, TracingTestClass()):
809-
with patch_start_tracing_child() as fake_start_child:
810-
assert instance_or_class.class_(1) == (TracingTestClass, 1)
811-
assert fake_start_child.call_count == 1
863+
(span,) = event["spans"]
864+
assert span["description"] == "tests.test_basics.TracingTestClass.class_"
812865

813866

814867
def test_last_event_id(sentry_init):

0 commit comments

Comments
 (0)