@@ -9,11 +9,13 @@ def my_fakefs_test(fs):
99 assert os.path.exists('/var/data/xx1.txt')
1010"""
1111
12+ import contextlib
13+
1214import py
1315import pytest
1416from _pytest import capture
1517
16- from pyfakefs .fake_filesystem_unittest import Patcher
18+ from pyfakefs .fake_filesystem_unittest import Patcher , Pause
1719
1820try :
1921 from _pytest import pathlib
@@ -85,22 +87,18 @@ def pytest_sessionfinish(session, exitstatus):
8587@pytest .hookimpl (hookwrapper = True , tryfirst = True )
8688def pytest_runtest_logreport (report ):
8789 """Make sure that patching is not active during reporting."""
88- pause = Patcher .PATCHER is not None and report .when == "call"
89- if pause :
90+ pause = Patcher .PATCHER is not None and Patcher .PATCHER .is_patching
91+ context_mgr = Pause (Patcher .PATCHER ) if pause else contextlib .nullcontext ()
92+ with context_mgr :
93+ yield
94+ if pause and report .when == "teardown" :
95+ # if we get here, we are not in a function scope fixture
96+ # in this case, we still want to pause patching between the tests
9097 Patcher .PATCHER .pause ()
91- yield
9298
9399
94- @pytest .hookimpl (hookwrapper = True , trylast = True )
100+ @pytest .hookimpl (tryfirst = True )
95101def pytest_runtest_setup (item ):
102+ # resume patcher if not in a function scope
96103 if Patcher .PATCHER is not None :
97104 Patcher .PATCHER .resume ()
98- yield
99-
100-
101- @pytest .hookimpl (hookwrapper = True , tryfirst = True )
102- def pytest_runtest_teardown (item , nextitem ):
103- """Make sure that patching is not active during reporting."""
104- if Patcher .PATCHER is not None :
105- Patcher .PATCHER .pause ()
106- yield
0 commit comments