55
66import pytest
77
8- from looptime import loops , patchers
8+ from looptime import loops , patchers , timeproxies
99
1010
11- @pytest .hookimpl (hookwrapper = True )
11+ @pytest .fixture ()
12+ def looptime () -> timeproxies .LoopTimeProxy :
13+ return timeproxies .LoopTimeProxy ()
14+
15+
16+ def pytest_configure (config : Any ) -> None :
17+ config .addinivalue_line ('markers' , "looptime: configure the fake fast-forwarding loop time." )
18+
19+
20+ def pytest_addoption (parser : Any ) -> None :
21+ group = parser .getgroup ("asyncio time contraction" )
22+ group .addoption ("--no-looptime" , dest = 'looptime' , action = "store_const" , const = False ,
23+ help = "Force all (even marked) tests to the true loop time." )
24+ group .addoption ("--looptime" , dest = 'looptime' , action = "store_const" , const = True ,
25+ help = "Run unmarked tests with the fake loop time by default." )
26+
27+
28+ @pytest .hookimpl (wrapper = True )
1229def pytest_fixture_setup (fixturedef : Any , request : Any ) -> Any :
30+ result = yield
31+
1332 if fixturedef .argname == "event_loop" :
14- result = yield
15- loop = cast (asyncio .BaseEventLoop , result .get_result ()) if result .excinfo is None else None
16- if loop is not None and not isinstance (loop , loops .LoopTimeEventLoop ):
33+ loop = cast (asyncio .BaseEventLoop , result )
34+ if not isinstance (loop , loops .LoopTimeEventLoop ):
1735
1836 # True means implicitly on; False means explicitly off; None means "only if marked".
1937 option : bool | None = request .config .getoption ('looptime' )
@@ -28,18 +46,6 @@ def pytest_fixture_setup(fixturedef: Any, request: Any) -> Any:
2846 if enabled :
2947 patched_loop = patchers .patch_event_loop (loop )
3048 patched_loop .setup_looptime (** options )
31- result .force_result (patched_loop )
32- else :
33- yield
34-
35-
36- def pytest_configure (config : Any ) -> None :
37- config .addinivalue_line ('markers' , "looptime: configure the fake fast-forwarding loop time." )
38-
49+ result = patched_loop
3950
40- def pytest_addoption (parser : Any ) -> None :
41- group = parser .getgroup ("asyncio time contraction" )
42- group .addoption ("--no-looptime" , dest = 'looptime' , action = "store_const" , const = False ,
43- help = "Force all (even marked) tests to the true loop time." )
44- group .addoption ("--looptime" , dest = 'looptime' , action = "store_const" , const = True ,
45- help = "Run unmarked tests with the fake loop time by default." )
51+ return result
0 commit comments