| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import inspect |
| 4 |
from typing import NoReturn |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from ..testing import MockClock, trio_test |
| 9 |
|
| 10 |
RUN_SLOW = True |
| 11 |
SKIP_OPTIONAL_IMPORTS = False |
| 12 |
|
| 13 |
|
| 14 |
def pytest_addoption(parser: pytest.Parser) -> None: |
| 15 |
parser.addoption("--run-slow", action="store_true", help="run slow tests") |
| 16 |
parser.addoption( |
| 17 |
"--skip-optional-imports", |
| 18 |
action="store_true", |
| 19 |
help="skip tests that rely on libraries not required by trio itself", |
| 20 |
) |
| 21 |
|
| 22 |
|
| 23 |
def pytest_configure(config: pytest.Config) -> None: |
| 24 |
global RUN_SLOW |
| 25 |
RUN_SLOW = config.getoption("--run-slow", default=True) |
| 26 |
global SKIP_OPTIONAL_IMPORTS |
| 27 |
SKIP_OPTIONAL_IMPORTS = config.getoption("--skip-optional-imports", default=False) |
| 28 |
|
| 29 |
|
| 30 |
@pytest.fixture |
| 31 |
def mock_clock() -> MockClock: |
| 32 |
return MockClock() |
| 33 |
|
| 34 |
|
| 35 |
@pytest.fixture |
| 36 |
def autojump_clock() -> MockClock: |
| 37 |
return MockClock(autojump_threshold=0) |
| 38 |
|
| 39 |
|
| 40 |
# FIXME: split off into a package (or just make part of Trio's public |
| 41 |
# interface?), with config file to enable? and I guess a mark option too; I |
| 42 |
# guess it's useful with the class- and file-level marking machinery (where |
| 43 |
# the raw @trio_test decorator isn't enough). |
| 44 |
@pytest.hookimpl(tryfirst=True) |
| 45 |
def pytest_pyfunc_call(pyfuncitem: pytest.Function) -> None: |
| 46 |
if inspect.iscoroutinefunction(pyfuncitem.obj): |
| 47 |
pyfuncitem.obj = trio_test(pyfuncitem.obj) |
| 48 |
|
| 49 |
|
| 50 |
def skip_if_optional_else_raise(error: ImportError) -> NoReturn: |
| 51 |
if SKIP_OPTIONAL_IMPORTS: |
| 52 |
pytest.skip(error.msg, allow_module_level=True) |
| 53 |
else: # pragma: no cover |
| 54 |
raise error |