@@ -371,38 +371,6 @@ def foo():
371
371
"--junit-xml=junit.xml" )
372
372
reprec .assertoutcome (failed = 1 )
373
373
374
- def test_doctest_module_session_fixture (self , testdir ):
375
- """Test that session fixtures are initialized for doctest modules (#768)
376
- """
377
- # session fixture which changes some global data, which will
378
- # be accessed by doctests in a module
379
- testdir .makeconftest ("""
380
- import pytest
381
- import sys
382
-
383
- @pytest.yield_fixture(autouse=True, scope='session')
384
- def myfixture():
385
- assert not hasattr(sys, 'pytest_session_data')
386
- sys.pytest_session_data = 1
387
- yield
388
- del sys.pytest_session_data
389
- """ )
390
- testdir .makepyfile (foo = """
391
- import sys
392
-
393
- def foo():
394
- '''
395
- >>> assert sys.pytest_session_data == 1
396
- '''
397
-
398
- def bar():
399
- '''
400
- >>> assert sys.pytest_session_data == 1
401
- '''
402
- """ )
403
- result = testdir .runpytest ("--doctest-modules" )
404
- result .stdout .fnmatch_lines ('*2 passed*' )
405
-
406
374
@pytest .mark .parametrize ('config_mode' , ['ini' , 'comment' ])
407
375
def test_allow_unicode (self , testdir , config_mode ):
408
376
"""Test that doctests which output unicode work in all python versions
@@ -446,7 +414,7 @@ def test_unicode_string(self, testdir):
446
414
reprec .assertoutcome (passed = passed , failed = int (not passed ))
447
415
448
416
449
- class TestDocTestSkips :
417
+ class TestDoctestSkips :
450
418
"""
451
419
If all examples in a doctest are skipped due to the SKIP option, then
452
420
the tests should be SKIPPED rather than PASSED. (#957)
@@ -493,3 +461,122 @@ def test_all_skipped(self, testdir, makedoctest):
493
461
""" )
494
462
reprec = testdir .inline_run ("--doctest-modules" )
495
463
reprec .assertoutcome (skipped = 1 )
464
+
465
+
466
+ class TestDoctestAutoUseFixtures :
467
+
468
+ SCOPES = ['module' , 'session' , 'class' , 'function' ]
469
+
470
+ def test_doctest_module_session_fixture (self , testdir ):
471
+ """Test that session fixtures are initialized for doctest modules (#768)
472
+ """
473
+ # session fixture which changes some global data, which will
474
+ # be accessed by doctests in a module
475
+ testdir .makeconftest ("""
476
+ import pytest
477
+ import sys
478
+
479
+ @pytest.yield_fixture(autouse=True, scope='session')
480
+ def myfixture():
481
+ assert not hasattr(sys, 'pytest_session_data')
482
+ sys.pytest_session_data = 1
483
+ yield
484
+ del sys.pytest_session_data
485
+ """ )
486
+ testdir .makepyfile (foo = """
487
+ import sys
488
+
489
+ def foo():
490
+ '''
491
+ >>> assert sys.pytest_session_data == 1
492
+ '''
493
+
494
+ def bar():
495
+ '''
496
+ >>> assert sys.pytest_session_data == 1
497
+ '''
498
+ """ )
499
+ result = testdir .runpytest ("--doctest-modules" )
500
+ result .stdout .fnmatch_lines ('*2 passed*' )
501
+
502
+ @pytest .mark .parametrize ('scope' , SCOPES )
503
+ @pytest .mark .parametrize ('enable_doctest' , [True , False ])
504
+ def test_fixture_scopes (self , testdir , scope , enable_doctest ):
505
+ """Test that auto-use fixtures work properly with doctest modules.
506
+ See #1057 and #1100.
507
+ """
508
+ testdir .makeconftest ('''
509
+ import pytest
510
+
511
+ @pytest.fixture(autouse=True, scope="{scope}")
512
+ def auto(request):
513
+ return 99
514
+ ''' .format (scope = scope ))
515
+ testdir .makepyfile (test_1 = '''
516
+ def test_foo():
517
+ """
518
+ >>> getfixture('auto') + 1
519
+ 100
520
+ """
521
+ def test_bar():
522
+ assert 1
523
+ ''' )
524
+ params = ('--doctest-modules' ,) if enable_doctest else ()
525
+ passes = 3 if enable_doctest else 2
526
+ result = testdir .runpytest (* params )
527
+ result .stdout .fnmatch_lines (['*=== %d passed in *' % passes ])
528
+
529
+ @pytest .mark .parametrize ('scope' , SCOPES )
530
+ @pytest .mark .parametrize ('autouse' , [True , False ])
531
+ @pytest .mark .parametrize ('use_fixture_in_doctest' , [True , False ])
532
+ def test_fixture_module_doctest_scopes (self , testdir , scope , autouse ,
533
+ use_fixture_in_doctest ):
534
+ """Test that auto-use fixtures work properly with doctest files.
535
+ See #1057 and #1100.
536
+ """
537
+ testdir .makeconftest ('''
538
+ import pytest
539
+
540
+ @pytest.fixture(autouse={autouse}, scope="{scope}")
541
+ def auto(request):
542
+ return 99
543
+ ''' .format (scope = scope , autouse = autouse ))
544
+ if use_fixture_in_doctest :
545
+ testdir .maketxtfile (test_doc = """
546
+ >>> getfixture('auto')
547
+ 99
548
+ """ )
549
+ else :
550
+ testdir .maketxtfile (test_doc = """
551
+ >>> 1 + 1
552
+ 2
553
+ """ )
554
+ result = testdir .runpytest ('--doctest-modules' )
555
+ assert 'FAILURES' not in str (result .stdout .str ())
556
+ result .stdout .fnmatch_lines (['*=== 1 passed in *' ])
557
+
558
+ @pytest .mark .parametrize ('scope' , SCOPES )
559
+ def test_auto_use_request_attributes (self , testdir , scope ):
560
+ """Check that all attributes of a request in an autouse fixture
561
+ behave as expected when requested for a doctest item.
562
+ """
563
+ testdir .makeconftest ('''
564
+ import pytest
565
+
566
+ @pytest.fixture(autouse=True, scope="{scope}")
567
+ def auto(request):
568
+ if "{scope}" == 'module':
569
+ assert request.module is None
570
+ if "{scope}" == 'class':
571
+ assert request.cls is None
572
+ if "{scope}" == 'function':
573
+ assert request.function is None
574
+ return 99
575
+ ''' .format (scope = scope ))
576
+ testdir .maketxtfile (test_doc = """
577
+ >>> 1 + 1
578
+ 2
579
+ """ )
580
+ result = testdir .runpytest ('--doctest-modules' )
581
+ assert 'FAILURES' not in str (result .stdout .str ())
582
+ result .stdout .fnmatch_lines (['*=== 1 passed in *' ])
0 commit comments