Skip to content

Commit 911b89e

Browse files
authored
fix: Less boxed testing (getsentry#522)
* fix: No boxed testing * fix: Un-break django tests * fix: Django 2.0 * fix: Work around weird pytest (?) bug * fix: Remove unused imports * fix: Fix new test * fix: Fix remainder of Django tests * fix: Upgrade tox * wip * fix: Use more forked markers * fix: Revert some diffs * fix: Set db.params * fix: Remove unused imports * fix: Fix django tests * fix: Contextvars are patching * fix: Fix celery freeze * fix: Fix threading tests
1 parent 68c3a64 commit 911b89e

File tree

10 files changed

+54
-20
lines changed

10 files changed

+54
-20
lines changed

pytest.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
[pytest]
22
DJANGO_SETTINGS_MODULE = tests.integrations.django.myapp.settings
3-
addopts = --boxed --tb=short
3+
addopts = --tb=short
44
markers = tests_internal_exceptions

test-requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
hypothesis==3.69.9
22
pytest==3.7.3
3-
pytest-xdist==1.23.0
3+
git+https://github.com/untitaker/pytest-forked@forked-marker#egg=pytest-forked
44
tox==3.7.0
55
Werkzeug==0.15.3
66
pytest-localserver==0.4.1

tests/conftest.py

+14-3
Original file line numberDiff line numberDiff line change
@@ -184,15 +184,22 @@ def fast_serialize(request):
184184

185185

186186
@pytest.fixture
187-
def sentry_init(monkeypatch_test_transport, fast_serialize):
187+
def sentry_init(monkeypatch_test_transport, fast_serialize, request):
188188
def inner(*a, **kw):
189189
hub = sentry_sdk.Hub.current
190190
client = sentry_sdk.Client(*a, **kw)
191191
client.options["_experiments"]["fast_serialize"] = fast_serialize
192192
hub.bind_client(client)
193193
monkeypatch_test_transport(sentry_sdk.Hub.current.client)
194194

195-
return inner
195+
if request.node.get_closest_marker("forked"):
196+
# Do not run isolation if the test is already running in
197+
# ultimate isolation (seems to be required for celery tests that
198+
# fork)
199+
yield inner
200+
else:
201+
with sentry_sdk.Hub(None):
202+
yield inner
196203

197204

198205
class TestTransport(Transport):
@@ -258,7 +265,11 @@ def read_flush(self):
258265

259266

260267
# scope=session ensures that fixture is run earlier
261-
@pytest.fixture(scope="session", params=[None, "eventlet", "gevent"])
268+
@pytest.fixture(
269+
scope="session",
270+
params=[None, "eventlet", "gevent"],
271+
ids=("threads", "eventlet", "greenlet"),
272+
)
262273
def maybe_monkeypatched_threading(request):
263274
if request.param == "eventlet":
264275
try:

tests/integrations/celery/test_celery.py

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ def dummy_task(self):
271271
assert e["type"] == "ZeroDivisionError"
272272

273273

274+
@pytest.mark.forked
274275
@pytest.mark.skipif(VERSION < (4,), reason="in-memory backend broken")
275276
def test_transport_shutdown(request, celery, capture_events_forksafe, tmpdir):
276277
events = capture_events_forksafe()

tests/integrations/django/test_basic.py

+17-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import absolute_import
2+
13
import pytest
24
import json
35

@@ -81,6 +83,7 @@ def test_transaction_with_class_view(sentry_init, client, capture_events):
8183
assert event["message"] == "hi"
8284

8385

86+
@pytest.mark.forked
8487
@pytest.mark.django_db
8588
def test_user_captured(sentry_init, client, capture_events):
8689
sentry_init(integrations=[DjangoIntegration()], send_default_pii=True)
@@ -102,6 +105,7 @@ def test_user_captured(sentry_init, client, capture_events):
102105
}
103106

104107

108+
@pytest.mark.forked
105109
@pytest.mark.django_db
106110
def test_queryset_repr(sentry_init, capture_events):
107111
sentry_init(integrations=[DjangoIntegration()])
@@ -156,6 +160,7 @@ def test_500(sentry_init, client, capture_events):
156160
assert content == "Sentry error: %s" % event_id
157161

158162

163+
@pytest.mark.forked
159164
def test_management_command_raises():
160165
# This just checks for our assumption that Django passes through all
161166
# exceptions by default, so our excepthook can be used for management
@@ -164,6 +169,7 @@ def test_management_command_raises():
164169
execute_from_command_line(["manage.py", "mycrash"])
165170

166171

172+
@pytest.mark.forked
167173
@pytest.mark.django_db
168174
@pytest.mark.parametrize("with_integration", [True, False])
169175
def test_sql_queries(sentry_init, capture_events, with_integration):
@@ -175,9 +181,16 @@ def test_sql_queries(sentry_init, capture_events, with_integration):
175181

176182
from django.db import connection
177183

178-
sql = connection.cursor()
184+
sentry_init(
185+
integrations=[DjangoIntegration()],
186+
send_default_pii=True,
187+
_experiments={"record_sql_params": True},
188+
)
179189

180190
events = capture_events()
191+
192+
sql = connection.cursor()
193+
181194
with pytest.raises(OperationalError):
182195
# table doesn't even exist
183196
sql.execute("""SELECT count(*) FROM people_person WHERE foo = %s""", [123])
@@ -193,6 +206,7 @@ def test_sql_queries(sentry_init, capture_events, with_integration):
193206
assert crumb["data"]["db.params"] == [123]
194207

195208

209+
@pytest.mark.forked
196210
@pytest.mark.django_db
197211
def test_sql_dict_query_params(sentry_init, capture_events):
198212
sentry_init(
@@ -234,6 +248,7 @@ def test_sql_dict_query_params(sentry_init, capture_events):
234248
lambda sql: sql.SQL('SELECT %(my_param)s FROM "foobar"'),
235249
],
236250
)
251+
@pytest.mark.forked
237252
@pytest.mark.django_db
238253
def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
239254
sentry_init(
@@ -262,6 +277,7 @@ def test_sql_psycopg2_string_composition(sentry_init, capture_events, query):
262277
assert crumb["data"]["db.params"] == {"my_param": 10}
263278

264279

280+
@pytest.mark.forked
265281
@pytest.mark.django_db
266282
def test_sql_psycopg2_placeholders(sentry_init, capture_events):
267283
sentry_init(

tests/integrations/threading/test_threading.py

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from sentry_sdk.integrations.threading import ThreadingIntegration
99

1010

11+
@pytest.mark.forked
1112
@pytest.mark.parametrize("integrations", [[ThreadingIntegration()], []])
1213
def test_handles_exceptions(sentry_init, capture_events, integrations):
1314
sentry_init(default_integrations=False, integrations=integrations)
@@ -30,6 +31,7 @@ def crash():
3031
assert not events
3132

3233

34+
@pytest.mark.forked
3335
@pytest.mark.parametrize("propagate_hub", (True, False))
3436
def test_propagates_hub(sentry_init, capture_events, propagate_hub):
3537
sentry_init(
@@ -85,6 +87,7 @@ def run(self):
8587
assert not gc.collect()
8688

8789

90+
@pytest.mark.forked
8891
def test_double_patching(sentry_init, capture_events):
8992
sentry_init(default_integrations=False, integrations=[ThreadingIntegration()])
9093
events = capture_events()

tests/test_basics.py

+9-14
Original file line numberDiff line numberDiff line change
@@ -205,26 +205,21 @@ def test_breadcrumbs(sentry_init, capture_events):
205205
assert len(event["breadcrumbs"]) == 0
206206

207207

208-
def test_integration_scoping():
208+
def test_integration_scoping(sentry_init, capture_events):
209209
logger = logging.getLogger("test_basics")
210-
events = []
211-
logging_integration = LoggingIntegration(event_level=logging.WARNING)
212210

213211
# This client uses the logging integration
214-
client_with_logging = Client(
215-
transport=events.append,
216-
default_integrations=False,
217-
integrations=[logging_integration],
218-
)
219-
Hub.current.bind_client(client_with_logging)
212+
logging_integration = LoggingIntegration(event_level=logging.WARNING)
213+
sentry_init(default_integrations=False, integrations=[logging_integration])
214+
events = capture_events()
220215
logger.warning("This is a warning")
216+
assert len(events) == 1
221217

222218
# This client does not
223-
client_without_logging = Client(transport=events.append, default_integrations=False)
224-
Hub.current.bind_client(client_without_logging)
219+
sentry_init(default_integrations=False)
220+
events = capture_events()
225221
logger.warning("This is not a warning")
226-
227-
assert len(events) == 1
222+
assert not events
228223

229224

230225
def test_client_initialized_within_scope(sentry_init, caplog):
@@ -233,7 +228,7 @@ def test_client_initialized_within_scope(sentry_init, caplog):
233228
sentry_init(debug=True)
234229

235230
with push_scope():
236-
sentry_init()
231+
Hub.current.bind_client(Client())
237232

238233
record, = (x for x in caplog.records if x.levelname == "WARNING")
239234

tests/test_client.py

+4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# coding: utf-8
2+
import os
23
import json
34
import pytest
45
import subprocess
@@ -31,6 +32,9 @@ def capture_event(self, event):
3132

3233

3334
def test_transport_option(monkeypatch):
35+
if "SENTRY_DSN" in os.environ:
36+
monkeypatch.delenv("SENTRY_DSN")
37+
3438
dsn = "https://foo@sentry.io/123"
3539
dsn2 = "https://bar@sentry.io/124"
3640
assert str(Client(dsn=dsn).dsn) == dsn

tests/test_transport.py

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def inner(*args, **kwargs):
2121
return inner
2222

2323

24+
@pytest.mark.forked
2425
@pytest.mark.parametrize("debug", (True, False))
2526
@pytest.mark.parametrize("client_flush_method", ["close", "flush"])
2627
def test_transport_works(

tests/utils/test_contextvars.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
1+
import pytest
12
import random
23
import time
34

45

56
from sentry_sdk.utils import _is_threading_local_monkey_patched
67

78

9+
@pytest.mark.forked
810
def test_thread_local_is_patched(maybe_monkeypatched_threading):
911
if maybe_monkeypatched_threading is None:
1012
assert not _is_threading_local_monkey_patched()
1113
else:
1214
assert _is_threading_local_monkey_patched()
1315

1416

17+
@pytest.mark.forked
1518
def test_leaks(maybe_monkeypatched_threading):
1619
import threading
1720

0 commit comments

Comments
 (0)