typing: pytest_collection
This commit is contained in:
parent
eb5e651d7e
commit
88b800355a
|
@ -7,6 +7,10 @@ from typing import Optional
|
||||||
from _pytest.assertion import rewrite
|
from _pytest.assertion import rewrite
|
||||||
from _pytest.assertion import truncate
|
from _pytest.assertion import truncate
|
||||||
from _pytest.assertion import util
|
from _pytest.assertion import util
|
||||||
|
from _pytest.compat import TYPE_CHECKING
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from _pytest.main import Session
|
||||||
|
|
||||||
|
|
||||||
def pytest_addoption(parser):
|
def pytest_addoption(parser):
|
||||||
|
@ -91,7 +95,7 @@ def install_importhook(config):
|
||||||
return hook
|
return hook
|
||||||
|
|
||||||
|
|
||||||
def pytest_collection(session):
|
def pytest_collection(session: "Session") -> None:
|
||||||
# this hook is only called when test modules are collected
|
# this hook is only called when test modules are collected
|
||||||
# so for example not in the master process of pytest-xdist
|
# so for example not in the master process of pytest-xdist
|
||||||
# (which does not collect test modules)
|
# (which does not collect test modules)
|
||||||
|
|
|
@ -1,6 +1,14 @@
|
||||||
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
|
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """
|
||||||
|
from typing import Any
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
from pluggy import HookspecMarker
|
from pluggy import HookspecMarker
|
||||||
|
|
||||||
|
from _pytest.compat import TYPE_CHECKING
|
||||||
|
|
||||||
|
if TYPE_CHECKING:
|
||||||
|
from _pytest.main import Session
|
||||||
|
|
||||||
|
|
||||||
hookspec = HookspecMarker("pytest")
|
hookspec = HookspecMarker("pytest")
|
||||||
|
|
||||||
|
@ -158,7 +166,7 @@ def pytest_load_initial_conftests(early_config, parser, args):
|
||||||
|
|
||||||
|
|
||||||
@hookspec(firstresult=True)
|
@hookspec(firstresult=True)
|
||||||
def pytest_collection(session):
|
def pytest_collection(session: "Session") -> Optional[Any]:
|
||||||
"""Perform the collection protocol for the given session.
|
"""Perform the collection protocol for the given session.
|
||||||
|
|
||||||
Stops at first non-None result, see :ref:`firstresult`.
|
Stops at first non-None result, see :ref:`firstresult`.
|
||||||
|
|
|
@ -5,6 +5,7 @@ from contextlib import contextmanager
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
from typing import AbstractSet
|
from typing import AbstractSet
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
from typing import Generator
|
||||||
from typing import List
|
from typing import List
|
||||||
from typing import Mapping
|
from typing import Mapping
|
||||||
|
|
||||||
|
@ -591,7 +592,7 @@ class LoggingPlugin:
|
||||||
) is not None or self._config.getini("log_cli")
|
) is not None or self._config.getini("log_cli")
|
||||||
|
|
||||||
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
|
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
|
||||||
def pytest_collection(self):
|
def pytest_collection(self) -> Generator[None, None, None]:
|
||||||
with self.live_logs_context():
|
with self.live_logs_context():
|
||||||
if self.log_cli_handler:
|
if self.log_cli_handler:
|
||||||
self.log_cli_handler.set_when("collection")
|
self.log_cli_handler.set_when("collection")
|
||||||
|
|
|
@ -8,6 +8,8 @@ import sys
|
||||||
from typing import Dict
|
from typing import Dict
|
||||||
from typing import FrozenSet
|
from typing import FrozenSet
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from typing import Optional
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import py
|
import py
|
||||||
|
@ -249,7 +251,7 @@ def pytest_cmdline_main(config):
|
||||||
return wrap_session(config, _main)
|
return wrap_session(config, _main)
|
||||||
|
|
||||||
|
|
||||||
def _main(config, session):
|
def _main(config: Config, session: "Session") -> Optional[Union[int, ExitCode]]:
|
||||||
""" default command line protocol for initialization, session,
|
""" default command line protocol for initialization, session,
|
||||||
running tests and reporting. """
|
running tests and reporting. """
|
||||||
config.hook.pytest_collection(session=session)
|
config.hook.pytest_collection(session=session)
|
||||||
|
@ -259,6 +261,7 @@ def _main(config, session):
|
||||||
return ExitCode.TESTS_FAILED
|
return ExitCode.TESTS_FAILED
|
||||||
elif session.testscollected == 0:
|
elif session.testscollected == 0:
|
||||||
return ExitCode.NO_TESTS_COLLECTED
|
return ExitCode.NO_TESTS_COLLECTED
|
||||||
|
return None
|
||||||
|
|
||||||
|
|
||||||
def pytest_collection(session):
|
def pytest_collection(session):
|
||||||
|
|
|
@ -514,7 +514,7 @@ class TerminalReporter:
|
||||||
# py < 1.6.0
|
# py < 1.6.0
|
||||||
return self._tw.chars_on_current_line
|
return self._tw.chars_on_current_line
|
||||||
|
|
||||||
def pytest_collection(self):
|
def pytest_collection(self) -> None:
|
||||||
if self.isatty:
|
if self.isatty:
|
||||||
if self.config.option.verbose >= 0:
|
if self.config.option.verbose >= 0:
|
||||||
self.write("collecting ... ", bold=True)
|
self.write("collecting ... ", bold=True)
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
import sys
|
import sys
|
||||||
import warnings
|
import warnings
|
||||||
from contextlib import contextmanager
|
from contextlib import contextmanager
|
||||||
|
from typing import Generator
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
from _pytest.main import Session
|
||||||
|
|
||||||
|
|
||||||
def _setoption(wmod, arg):
|
def _setoption(wmod, arg):
|
||||||
|
@ -117,7 +119,7 @@ def pytest_runtest_protocol(item):
|
||||||
|
|
||||||
|
|
||||||
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
|
@pytest.hookimpl(hookwrapper=True, tryfirst=True)
|
||||||
def pytest_collection(session):
|
def pytest_collection(session: Session) -> Generator[None, None, None]:
|
||||||
config = session.config
|
config = session.config
|
||||||
with catch_warnings_for_item(
|
with catch_warnings_for_item(
|
||||||
config=config, ihook=config.hook, when="collect", item=None
|
config=config, ihook=config.hook, when="collect", item=None
|
||||||
|
|
Loading…
Reference in New Issue