@@ -701,3 +701,91 @@ def test_foo():
701701
702702 time .sleep .assert_called_with (5 )
703703 assert_outcomes (result , passed = 0 , failed = 1 , rerun = 4 )
704+
705+
706+ def test_run_session_teardown_once_after_reruns (testdir ):
707+ testdir .makepyfile (
708+ """
709+ import logging
710+ import pytest
711+
712+ @pytest.fixture(scope='session')
713+ def session_fixture():
714+ logging.info('session setup')
715+ yield
716+ logging.info('session teardown')
717+
718+ @pytest.fixture(scope='class')
719+ def class_fixture():
720+ logging.info('class setup')
721+ yield
722+ logging.info('class teardown')
723+
724+ @pytest.fixture(scope='function')
725+ def function_fixture():
726+ logging.info('function setup')
727+ yield
728+ logging.info('function teardown')
729+
730+ class TestFoo:
731+ @staticmethod
732+ def test_foo_1(session_fixture, class_fixture, function_fixture):
733+ pass
734+
735+ @staticmethod
736+ def test_foo_2(session_fixture, class_fixture, function_fixture):
737+ assert False
738+
739+ class TestBar:
740+ @staticmethod
741+ def test_bar_1(session_fixture, class_fixture, function_fixture):
742+ assert False
743+
744+ @staticmethod
745+ def test_bar_2(session_fixture, class_fixture, function_fixture):
746+ assert False
747+
748+ @staticmethod
749+ def test_bar_3(session_fixture, class_fixture, function_fixture):
750+ pass"""
751+ )
752+ import logging
753+
754+ logging .info = mock .MagicMock ()
755+
756+ result = testdir .runpytest ("--reruns" , "2" )
757+ expected_calls = [
758+ mock .call ("session setup" ),
759+ # class TestFoo
760+ mock .call ("class setup" ),
761+ mock .call ("function setup" ),
762+ mock .call ("function teardown" ),
763+ mock .call ("function setup" ),
764+ mock .call ("function teardown" ),
765+ mock .call ("function setup" ),
766+ mock .call ("function teardown" ),
767+ mock .call ("function setup" ),
768+ mock .call ("function teardown" ),
769+ mock .call ("class teardown" ),
770+ # class TestBar
771+ mock .call ("class setup" ),
772+ mock .call ("function setup" ),
773+ mock .call ("function teardown" ),
774+ mock .call ("function setup" ),
775+ mock .call ("function teardown" ),
776+ mock .call ("function setup" ),
777+ mock .call ("function teardown" ),
778+ mock .call ("function setup" ),
779+ mock .call ("function teardown" ),
780+ mock .call ("function setup" ),
781+ mock .call ("function teardown" ),
782+ mock .call ("function setup" ),
783+ mock .call ("function teardown" ),
784+ mock .call ("function setup" ),
785+ mock .call ("function teardown" ),
786+ mock .call ("class teardown" ),
787+ mock .call ("session teardown" ),
788+ ]
789+
790+ logging .info .assert_has_calls (expected_calls , any_order = False )
791+ assert_outcomes (result , failed = 3 , passed = 2 , rerun = 6 )
0 commit comments