Skip to content

Commit 2d1c034

Browse files
authored
Merge pull request #8 from nolar/newer-pytest
Utilise a few newer features ot pytest/pluggy
2 parents 257adf7 + 1854c26 commit 2d1c034

File tree

2 files changed

+25
-29
lines changed

2 files changed

+25
-29
lines changed

looptime/plugin.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,33 @@
55

66
import 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)
1229
def 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

looptime/timeproxies.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,3 @@ def __matmul__(self, other: object) -> LoopTimeProxy:
3131
@property
3232
def _value(self) -> float:
3333
return self._loop.time() if self._loop is not None else asyncio.get_running_loop().time()
34-
35-
36-
try:
37-
import pytest
38-
except ImportError:
39-
pass
40-
else:
41-
@pytest.fixture()
42-
def looptime() -> LoopTimeProxy:
43-
return LoopTimeProxy()

0 commit comments

Comments
 (0)