Skip to content

Commit 0ee6a25

Browse files
authored
Use sentry_init fixture in tests instead of using Hub directly (getsentry#759)
1 parent 7898eac commit 0ee6a25

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

tests/conftest.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ def inner(*a, **kw):
186186
hub = sentry_sdk.Hub.current
187187
client = sentry_sdk.Client(*a, **kw)
188188
hub.bind_client(client)
189-
monkeypatch_test_transport(sentry_sdk.Hub.current.client)
189+
if "transport" not in kw:
190+
monkeypatch_test_transport(sentry_sdk.Hub.current.client)
190191

191192
if request.node.get_closest_marker("forked"):
192193
# Do not run isolation if the test is already running in

tests/test_client.py

Lines changed: 39 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
import time
88

99
from textwrap import dedent
10-
from sentry_sdk import Hub, Client, configure_scope, capture_message, capture_exception
10+
from sentry_sdk import (
11+
Hub,
12+
Client,
13+
configure_scope,
14+
capture_message,
15+
capture_exception,
16+
capture_event,
17+
)
1118
from sentry_sdk.transport import Transport
1219
from sentry_sdk._compat import reraise, text_type, PY2
1320
from sentry_sdk.utils import HAS_CHAINED_EXCEPTIONS
@@ -149,41 +156,41 @@ def test_proxy_httpsselect_bothenv_http(monkeypatch):
149156
assert client.transport._pool.proxy.scheme == "http"
150157

151158

152-
def test_simple_transport():
159+
def test_simple_transport(sentry_init):
153160
events = []
154-
with Hub(Client(transport=events.append)):
155-
capture_message("Hello World!")
161+
sentry_init(transport=events.append)
162+
capture_message("Hello World!")
156163
assert events[0]["message"] == "Hello World!"
157164

158165

159-
def test_ignore_errors():
166+
def test_ignore_errors(sentry_init, capture_events):
160167
class MyDivisionError(ZeroDivisionError):
161168
pass
162169

163170
def raise_it(exc_info):
164171
reraise(*exc_info)
165172

166-
hub = Hub(Client(ignore_errors=[ZeroDivisionError], transport=_TestTransport()))
167-
hub._capture_internal_exception = raise_it
173+
sentry_init(ignore_errors=[ZeroDivisionError], transport=_TestTransport())
174+
Hub.current._capture_internal_exception = raise_it
168175

169176
def e(exc):
170177
try:
171178
raise exc
172179
except Exception:
173-
hub.capture_exception()
180+
capture_exception()
174181

175182
e(ZeroDivisionError())
176183
e(MyDivisionError())
177184
pytest.raises(EventCaptured, lambda: e(ValueError()))
178185

179186

180-
def test_with_locals_enabled():
181-
events = []
182-
hub = Hub(Client(with_locals=True, transport=events.append))
187+
def test_with_locals_enabled(sentry_init, capture_events):
188+
sentry_init(with_locals=True)
189+
events = capture_events()
183190
try:
184191
1 / 0
185192
except Exception:
186-
hub.capture_exception()
193+
capture_exception()
187194

188195
(event,) = events
189196

@@ -193,13 +200,13 @@ def test_with_locals_enabled():
193200
)
194201

195202

196-
def test_with_locals_disabled():
197-
events = []
198-
hub = Hub(Client(with_locals=False, transport=events.append))
203+
def test_with_locals_disabled(sentry_init, capture_events):
204+
sentry_init(with_locals=False)
205+
events = capture_events()
199206
try:
200207
1 / 0
201208
except Exception:
202-
hub.capture_exception()
209+
capture_exception()
203210

204211
(event,) = events
205212

@@ -209,15 +216,15 @@ def test_with_locals_disabled():
209216
)
210217

211218

212-
def test_attach_stacktrace_enabled():
213-
events = []
214-
hub = Hub(Client(attach_stacktrace=True, transport=events.append))
219+
def test_attach_stacktrace_enabled(sentry_init, capture_events):
220+
sentry_init(attach_stacktrace=True)
221+
events = capture_events()
215222

216223
def foo():
217224
bar()
218225

219226
def bar():
220-
hub.capture_message("HI")
227+
capture_message("HI")
221228

222229
foo()
223230

@@ -227,17 +234,15 @@ def bar():
227234
assert functions[-2:] == ["foo", "bar"]
228235

229236

230-
def test_attach_stacktrace_enabled_no_locals():
231-
events = []
232-
hub = Hub(
233-
Client(attach_stacktrace=True, with_locals=False, transport=events.append)
234-
)
237+
def test_attach_stacktrace_enabled_no_locals(sentry_init, capture_events):
238+
sentry_init(attach_stacktrace=True, with_locals=False)
239+
events = capture_events()
235240

236241
def foo():
237242
bar()
238243

239244
def bar():
240-
hub.capture_message("HI")
245+
capture_message("HI")
241246

242247
foo()
243248

@@ -262,19 +267,19 @@ def test_attach_stacktrace_in_app(sentry_init, capture_events):
262267
assert any(f["in_app"] for f in frames)
263268

264269

265-
def test_attach_stacktrace_disabled():
266-
events = []
267-
hub = Hub(Client(attach_stacktrace=False, transport=events.append))
268-
hub.capture_message("HI")
270+
def test_attach_stacktrace_disabled(sentry_init, capture_events):
271+
sentry_init(attach_stacktrace=False)
272+
events = capture_events()
273+
capture_message("HI")
269274

270275
(event,) = events
271276
assert "threads" not in event
272277

273278

274-
def test_capture_event_works():
275-
c = Client(transport=_TestTransport())
276-
pytest.raises(EventCaptured, lambda: c.capture_event({}))
277-
pytest.raises(EventCaptured, lambda: c.capture_event({}))
279+
def test_capture_event_works(sentry_init):
280+
sentry_init(transport=_TestTransport())
281+
pytest.raises(EventCaptured, lambda: capture_event({}))
282+
pytest.raises(EventCaptured, lambda: capture_event({}))
278283

279284

280285
@pytest.mark.parametrize("num_messages", [10, 20])

0 commit comments

Comments
 (0)