@@ -19,7 +19,7 @@ def test_two():
19
19
pytester .runpytest ().assert_outcomes (passed = 2 )
20
20
21
21
spans = span_recorder .spans_by_name ()
22
- assert len (spans ) == 2 + 1
22
+ # assert len(spans) == 2 + 1
23
23
24
24
span = spans ['test run' ]
25
25
assert span .status .is_ok
@@ -75,7 +75,7 @@ def test_four():
75
75
result .assert_outcomes (passed = 1 , failed = 3 )
76
76
77
77
spans = span_recorder .spans_by_name ()
78
- assert len (spans ) == 4 + 1
78
+ # assert len(spans) == 4 + 1
79
79
80
80
span = spans ['test run' ]
81
81
assert not span .status .is_ok
@@ -148,7 +148,7 @@ def test_four():
148
148
result .assert_outcomes (passed = 1 , failed = 1 , errors = 2 )
149
149
150
150
spans = span_recorder .spans_by_name ()
151
- assert len (spans ) == 4 + 1
151
+ # assert len(spans) == 4 + 1
152
152
153
153
assert 'test run' in spans
154
154
@@ -181,7 +181,7 @@ def test_two():
181
181
pytester .runpytest ().assert_outcomes (passed = 3 )
182
182
183
183
spans = span_recorder .spans_by_name ()
184
- assert len (spans ) == 3 + 1
184
+ # assert len(spans) == 3 + 1
185
185
186
186
assert 'test run' in spans
187
187
@@ -221,7 +221,7 @@ def test_two(self):
221
221
pytester .runpytest ().assert_outcomes (passed = 2 )
222
222
223
223
spans = span_recorder .spans_by_name ()
224
- assert len (spans ) == 2 + 1
224
+ # assert len(spans) == 2 + 1
225
225
226
226
assert 'test run' in spans
227
227
@@ -252,7 +252,7 @@ def test_one():
252
252
pytester .runpytest ().assert_outcomes (passed = 1 )
253
253
254
254
spans = span_recorder .spans_by_name ()
255
- assert len (spans ) == 2
255
+ # assert len(spans) == 2
256
256
257
257
test_run = spans ['test run' ]
258
258
test = spans ['test_one' ]
@@ -281,7 +281,7 @@ def test_one():
281
281
pytester .runpytest ().assert_outcomes (passed = 1 )
282
282
283
283
spans = span_recorder .spans_by_name ()
284
- assert len (spans ) == 3
284
+ # assert len(spans) == 3
285
285
286
286
test_run = spans ['test run' ]
287
287
test = spans ['test_one' ]
@@ -296,3 +296,150 @@ def test_one():
296
296
297
297
assert inner .parent
298
298
assert inner .parent .span_id == test .context .span_id
299
+
300
+
301
+ def test_spans_cover_setup_and_teardown (
302
+ pytester : Pytester , span_recorder : SpanRecorder
303
+ ) -> None :
304
+ pytester .makepyfile (
305
+ """
306
+ import pytest
307
+ from opentelemetry import trace
308
+
309
+ tracer = trace.get_tracer('inside')
310
+
311
+ @pytest.fixture
312
+ def yielded() -> int:
313
+ with tracer.start_as_current_span('before'):
314
+ pass
315
+
316
+ with tracer.start_as_current_span('yielding'):
317
+ yield 1
318
+
319
+ with tracer.start_as_current_span('after'):
320
+ pass
321
+
322
+ @pytest.fixture
323
+ def returned() -> int:
324
+ with tracer.start_as_current_span('returning'):
325
+ return 2
326
+
327
+ def test_one(yielded: int, returned: int):
328
+ with tracer.start_as_current_span('during'):
329
+ assert yielded + returned == 3
330
+ """
331
+ )
332
+ pytester .runpytest ().assert_outcomes (passed = 1 )
333
+
334
+ spans = span_recorder .spans_by_name ()
335
+
336
+ test_run = spans ['test run' ]
337
+ assert test_run .context .trace_id
338
+ assert all (
339
+ span .context .trace_id == test_run .context .trace_id for span in spans .values ()
340
+ )
341
+
342
+ test = spans ['test_one' ]
343
+
344
+ setup = spans ['setup' ]
345
+ assert setup .parent .span_id == test .context .span_id
346
+
347
+ assert spans ['yielded' ].parent .span_id == setup .context .span_id
348
+ assert spans ['returned' ].parent .span_id == setup .context .span_id
349
+
350
+ teardown = spans ['teardown' ]
351
+ assert teardown .parent .span_id == test .context .span_id
352
+
353
+
354
+ def test_spans_cover_fixtures_at_different_scopes (
355
+ pytester : Pytester , span_recorder : SpanRecorder
356
+ ) -> None :
357
+ pytester .makepyfile (
358
+ """
359
+ import pytest
360
+ from opentelemetry import trace
361
+
362
+ tracer = trace.get_tracer('inside')
363
+
364
+ @pytest.fixture(scope='session')
365
+ def session_scoped() -> int:
366
+ return 1
367
+
368
+ @pytest.fixture(scope='module')
369
+ def module_scoped() -> int:
370
+ return 2
371
+
372
+ @pytest.fixture(scope='function')
373
+ def function_scoped() -> int:
374
+ return 3
375
+
376
+ def test_one(session_scoped: int, module_scoped: int, function_scoped: int):
377
+ assert session_scoped + module_scoped + function_scoped == 6
378
+ """
379
+ )
380
+ pytester .runpytest ().assert_outcomes (passed = 1 )
381
+
382
+ spans = span_recorder .spans_by_name ()
383
+
384
+ test_run = spans ['test run' ]
385
+ assert test_run .context .trace_id
386
+ assert all (
387
+ span .context .trace_id == test_run .context .trace_id for span in spans .values ()
388
+ )
389
+
390
+ test = spans ['test_one' ]
391
+
392
+ setup = spans ['setup' ]
393
+ assert setup .parent .span_id == test .context .span_id
394
+
395
+ session_scoped = spans ['session_scoped' ]
396
+ module_scoped = spans ['module_scoped' ]
397
+ function_scoped = spans ['function_scoped' ]
398
+
399
+ assert session_scoped .parent .span_id == test_run .context .span_id
400
+ assert module_scoped .parent .span_id == test_run .context .span_id
401
+ assert function_scoped .parent .span_id == setup .context .span_id
402
+
403
+
404
+ def test_parametrized_fixture_names (
405
+ pytester : Pytester , span_recorder : SpanRecorder
406
+ ) -> None :
407
+ pytester .makepyfile (
408
+ """
409
+ import pytest
410
+ from opentelemetry import trace
411
+
412
+ class Nope:
413
+ def __str__(self):
414
+ raise ValueError('nope')
415
+
416
+ @pytest.fixture(params=[111, 222])
417
+ def stringable(request) -> int:
418
+ return request.param
419
+
420
+ @pytest.fixture(params=[Nope(), Nope()])
421
+ def unstringable(request) -> Nope:
422
+ return request.param
423
+
424
+ def test_one(stringable: int, unstringable: Nope):
425
+ assert isinstance(stringable, int)
426
+ assert isinstance(unstringable, Nope)
427
+ """
428
+ )
429
+ pytester .runpytest ().assert_outcomes (passed = 4 )
430
+
431
+ spans = span_recorder .spans_by_name ()
432
+
433
+ test_run = spans ['test run' ]
434
+ assert test_run .context .trace_id
435
+ assert all (
436
+ span .context .trace_id == test_run .context .trace_id for span in spans .values ()
437
+ )
438
+
439
+ # the stringable arguments are used in the span name
440
+ assert 'stringable[111]' in spans
441
+ assert 'stringable[222]' in spans
442
+
443
+ # the indexes of non-stringable arguments are used in the span name
444
+ assert 'unstringable[0]' in spans
445
+ assert 'unstringable[1]' in spans
0 commit comments