7
7
import time
8
8
9
9
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
+ )
11
18
from sentry_sdk .transport import Transport
12
19
from sentry_sdk ._compat import reraise , text_type , PY2
13
20
from sentry_sdk .utils import HAS_CHAINED_EXCEPTIONS
@@ -149,41 +156,41 @@ def test_proxy_httpsselect_bothenv_http(monkeypatch):
149
156
assert client .transport ._pool .proxy .scheme == "http"
150
157
151
158
152
- def test_simple_transport ():
159
+ def test_simple_transport (sentry_init ):
153
160
events = []
154
- with Hub ( Client ( transport = events .append )):
155
- capture_message ("Hello World!" )
161
+ sentry_init ( transport = events .append )
162
+ capture_message ("Hello World!" )
156
163
assert events [0 ]["message" ] == "Hello World!"
157
164
158
165
159
- def test_ignore_errors ():
166
+ def test_ignore_errors (sentry_init , capture_events ):
160
167
class MyDivisionError (ZeroDivisionError ):
161
168
pass
162
169
163
170
def raise_it (exc_info ):
164
171
reraise (* exc_info )
165
172
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
168
175
169
176
def e (exc ):
170
177
try :
171
178
raise exc
172
179
except Exception :
173
- hub . capture_exception ()
180
+ capture_exception ()
174
181
175
182
e (ZeroDivisionError ())
176
183
e (MyDivisionError ())
177
184
pytest .raises (EventCaptured , lambda : e (ValueError ()))
178
185
179
186
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 ( )
183
190
try :
184
191
1 / 0
185
192
except Exception :
186
- hub . capture_exception ()
193
+ capture_exception ()
187
194
188
195
(event ,) = events
189
196
@@ -193,13 +200,13 @@ def test_with_locals_enabled():
193
200
)
194
201
195
202
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 ( )
199
206
try :
200
207
1 / 0
201
208
except Exception :
202
- hub . capture_exception ()
209
+ capture_exception ()
203
210
204
211
(event ,) = events
205
212
@@ -209,15 +216,15 @@ def test_with_locals_disabled():
209
216
)
210
217
211
218
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 ( )
215
222
216
223
def foo ():
217
224
bar ()
218
225
219
226
def bar ():
220
- hub . capture_message ("HI" )
227
+ capture_message ("HI" )
221
228
222
229
foo ()
223
230
@@ -227,17 +234,15 @@ def bar():
227
234
assert functions [- 2 :] == ["foo" , "bar" ]
228
235
229
236
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 ()
235
240
236
241
def foo ():
237
242
bar ()
238
243
239
244
def bar ():
240
- hub . capture_message ("HI" )
245
+ capture_message ("HI" )
241
246
242
247
foo ()
243
248
@@ -262,19 +267,19 @@ def test_attach_stacktrace_in_app(sentry_init, capture_events):
262
267
assert any (f ["in_app" ] for f in frames )
263
268
264
269
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" )
269
274
270
275
(event ,) = events
271
276
assert "threads" not in event
272
277
273
278
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 ({}))
278
283
279
284
280
285
@pytest .mark .parametrize ("num_messages" , [10 , 20 ])
0 commit comments