| 1 |
# mypy: allow-untyped-defs |
| 2 |
"""Hook specifications for pytest plugins which are invoked by pytest itself |
| 3 |
and by builtin plugins.""" |
| 4 |
|
| 5 |
from pathlib import Path |
| 6 |
from typing import Any |
| 7 |
from typing import Dict |
| 8 |
from typing import List |
| 9 |
from typing import Mapping |
| 10 |
from typing import Optional |
| 11 |
from typing import Sequence |
| 12 |
from typing import Tuple |
| 13 |
from typing import TYPE_CHECKING |
| 14 |
from typing import Union |
| 15 |
|
| 16 |
from pluggy import HookspecMarker |
| 17 |
|
| 18 |
from .deprecated import HOOK_LEGACY_PATH_ARG |
| 19 |
|
| 20 |
|
| 21 |
if TYPE_CHECKING: |
| 22 |
import pdb |
| 23 |
from typing import Literal |
| 24 |
import warnings |
| 25 |
|
| 26 |
from _pytest._code.code import ExceptionInfo |
| 27 |
from _pytest._code.code import ExceptionRepr |
| 28 |
from _pytest.compat import LEGACY_PATH |
| 29 |
from _pytest.config import _PluggyPlugin |
| 30 |
from _pytest.config import Config |
| 31 |
from _pytest.config import ExitCode |
| 32 |
from _pytest.config import PytestPluginManager |
| 33 |
from _pytest.config.argparsing import Parser |
| 34 |
from _pytest.fixtures import FixtureDef |
| 35 |
from _pytest.fixtures import SubRequest |
| 36 |
from _pytest.main import Session |
| 37 |
from _pytest.nodes import Collector |
| 38 |
from _pytest.nodes import Item |
| 39 |
from _pytest.outcomes import Exit |
| 40 |
from _pytest.python import Class |
| 41 |
from _pytest.python import Function |
| 42 |
from _pytest.python import Metafunc |
| 43 |
from _pytest.python import Module |
| 44 |
from _pytest.reports import CollectReport |
| 45 |
from _pytest.reports import TestReport |
| 46 |
from _pytest.runner import CallInfo |
| 47 |
from _pytest.terminal import TerminalReporter |
| 48 |
from _pytest.terminal import TestShortLogReport |
| 49 |
|
| 50 |
|
| 51 |
hookspec = HookspecMarker("pytest") |
| 52 |
|
| 53 |
# ------------------------------------------------------------------------- |
| 54 |
# Initialization hooks called for every plugin |
| 55 |
# ------------------------------------------------------------------------- |
| 56 |
|
| 57 |
|
| 58 |
@hookspec(historic=True) |
| 59 |
def pytest_addhooks(pluginmanager: "PytestPluginManager") -> None: |
| 60 |
"""Called at plugin registration time to allow adding new hooks via a call to |
| 61 |
:func:`pluginmanager.add_hookspecs(module_or_class, prefix) <pytest.PytestPluginManager.add_hookspecs>`. |
| 62 |
|
| 63 |
:param pluginmanager: The pytest plugin manager. |
| 64 |
|
| 65 |
.. note:: |
| 66 |
This hook is incompatible with hook wrappers. |
| 67 |
|
| 68 |
Use in conftest plugins |
| 69 |
======================= |
| 70 |
|
| 71 |
If a conftest plugin implements this hook, it will be called immediately |
| 72 |
when the conftest is registered. |
| 73 |
""" |
| 74 |
|
| 75 |
|
| 76 |
@hookspec(historic=True) |
| 77 |
def pytest_plugin_registered( |
| 78 |
plugin: "_PluggyPlugin", |
| 79 |
plugin_name: str, |
| 80 |
manager: "PytestPluginManager", |
| 81 |
) -> None: |
| 82 |
"""A new pytest plugin got registered. |
| 83 |
|
| 84 |
:param plugin: The plugin module or instance. |
| 85 |
:param plugin_name: The name by which the plugin is registered. |
| 86 |
:param manager: The pytest plugin manager. |
| 87 |
|
| 88 |
.. note:: |
| 89 |
This hook is incompatible with hook wrappers. |
| 90 |
|
| 91 |
Use in conftest plugins |
| 92 |
======================= |
| 93 |
|
| 94 |
If a conftest plugin implements this hook, it will be called immediately |
| 95 |
when the conftest is registered, once for each plugin registered thus far |
| 96 |
(including itself!), and for all plugins thereafter when they are |
| 97 |
registered. |
| 98 |
""" |
| 99 |
|
| 100 |
|
| 101 |
@hookspec(historic=True) |
| 102 |
def pytest_addoption(parser: "Parser", pluginmanager: "PytestPluginManager") -> None: |
| 103 |
"""Register argparse-style options and ini-style config values, |
| 104 |
called once at the beginning of a test run. |
| 105 |
|
| 106 |
:param parser: |
| 107 |
To add command line options, call |
| 108 |
:py:func:`parser.addoption(...) <pytest.Parser.addoption>`. |
| 109 |
To add ini-file values call :py:func:`parser.addini(...) |
| 110 |
<pytest.Parser.addini>`. |
| 111 |
|
| 112 |
:param pluginmanager: |
| 113 |
The pytest plugin manager, which can be used to install :py:func:`~pytest.hookspec`'s |
| 114 |
or :py:func:`~pytest.hookimpl`'s and allow one plugin to call another plugin's hooks |
| 115 |
to change how command line options are added. |
| 116 |
|
| 117 |
Options can later be accessed through the |
| 118 |
:py:class:`config <pytest.Config>` object, respectively: |
| 119 |
|
| 120 |
- :py:func:`config.getoption(name) <pytest.Config.getoption>` to |
| 121 |
retrieve the value of a command line option. |
| 122 |
|
| 123 |
- :py:func:`config.getini(name) <pytest.Config.getini>` to retrieve |
| 124 |
a value read from an ini-style file. |
| 125 |
|
| 126 |
The config object is passed around on many internal objects via the ``.config`` |
| 127 |
attribute or can be retrieved as the ``pytestconfig`` fixture. |
| 128 |
|
| 129 |
.. note:: |
| 130 |
This hook is incompatible with hook wrappers. |
| 131 |
|
| 132 |
Use in conftest plugins |
| 133 |
======================= |
| 134 |
|
| 135 |
If a conftest plugin implements this hook, it will be called immediately |
| 136 |
when the conftest is registered. |
| 137 |
|
| 138 |
This hook is only called for :ref:`initial conftests <pluginorder>`. |
| 139 |
""" |
| 140 |
|
| 141 |
|
| 142 |
@hookspec(historic=True) |
| 143 |
def pytest_configure(config: "Config") -> None: |
| 144 |
"""Allow plugins and conftest files to perform initial configuration. |
| 145 |
|
| 146 |
.. note:: |
| 147 |
This hook is incompatible with hook wrappers. |
| 148 |
|
| 149 |
:param config: The pytest config object. |
| 150 |
|
| 151 |
Use in conftest plugins |
| 152 |
======================= |
| 153 |
|
| 154 |
This hook is called for every :ref:`initial conftest <pluginorder>` file |
| 155 |
after command line options have been parsed. After that, the hook is called |
| 156 |
for other conftest files as they are registered. |
| 157 |
""" |
| 158 |
|
| 159 |
|
| 160 |
# ------------------------------------------------------------------------- |
| 161 |
# Bootstrapping hooks called for plugins registered early enough: |
| 162 |
# internal and 3rd party plugins. |
| 163 |
# ------------------------------------------------------------------------- |
| 164 |
|
| 165 |
|
| 166 |
@hookspec(firstresult=True) |
| 167 |
def pytest_cmdline_parse( |
| 168 |
pluginmanager: "PytestPluginManager", args: List[str] |
| 169 |
) -> Optional["Config"]: |
| 170 |
"""Return an initialized :class:`~pytest.Config`, parsing the specified args. |
| 171 |
|
| 172 |
Stops at first non-None result, see :ref:`firstresult`. |
| 173 |
|
| 174 |
.. note:: |
| 175 |
This hook is only called for plugin classes passed to the |
| 176 |
``plugins`` arg when using `pytest.main`_ to perform an in-process |
| 177 |
test run. |
| 178 |
|
| 179 |
:param pluginmanager: The pytest plugin manager. |
| 180 |
:param args: List of arguments passed on the command line. |
| 181 |
:returns: A pytest config object. |
| 182 |
|
| 183 |
Use in conftest plugins |
| 184 |
======================= |
| 185 |
|
| 186 |
This hook is not called for conftest files. |
| 187 |
""" |
| 188 |
|
| 189 |
|
| 190 |
def pytest_load_initial_conftests( |
| 191 |
early_config: "Config", parser: "Parser", args: List[str] |
| 192 |
) -> None: |
| 193 |
"""Called to implement the loading of :ref:`initial conftest files |
| 194 |
<pluginorder>` ahead of command line option parsing. |
| 195 |
|
| 196 |
:param early_config: The pytest config object. |
| 197 |
:param args: Arguments passed on the command line. |
| 198 |
:param parser: To add command line options. |
| 199 |
|
| 200 |
Use in conftest plugins |
| 201 |
======================= |
| 202 |
|
| 203 |
This hook is not called for conftest files. |
| 204 |
""" |
| 205 |
|
| 206 |
|
| 207 |
@hookspec(firstresult=True) |
| 208 |
def pytest_cmdline_main(config: "Config") -> Optional[Union["ExitCode", int]]: |
| 209 |
"""Called for performing the main command line action. |
| 210 |
|
| 211 |
The default implementation will invoke the configure hooks and |
| 212 |
:hook:`pytest_runtestloop`. |
| 213 |
|
| 214 |
Stops at first non-None result, see :ref:`firstresult`. |
| 215 |
|
| 216 |
:param config: The pytest config object. |
| 217 |
:returns: The exit code. |
| 218 |
|
| 219 |
Use in conftest plugins |
| 220 |
======================= |
| 221 |
|
| 222 |
This hook is only called for :ref:`initial conftests <pluginorder>`. |
| 223 |
""" |
| 224 |
|
| 225 |
|
| 226 |
# ------------------------------------------------------------------------- |
| 227 |
# collection hooks |
| 228 |
# ------------------------------------------------------------------------- |
| 229 |
|
| 230 |
|
| 231 |
@hookspec(firstresult=True) |
| 232 |
def pytest_collection(session: "Session") -> Optional[object]: |
| 233 |
"""Perform the collection phase for the given session. |
| 234 |
|
| 235 |
Stops at first non-None result, see :ref:`firstresult`. |
| 236 |
The return value is not used, but only stops further processing. |
| 237 |
|
| 238 |
The default collection phase is this (see individual hooks for full details): |
| 239 |
|
| 240 |
1. Starting from ``session`` as the initial collector: |
| 241 |
|
| 242 |
1. ``pytest_collectstart(collector)`` |
| 243 |
2. ``report = pytest_make_collect_report(collector)`` |
| 244 |
3. ``pytest_exception_interact(collector, call, report)`` if an interactive exception occurred |
| 245 |
4. For each collected node: |
| 246 |
|
| 247 |
1. If an item, ``pytest_itemcollected(item)`` |
| 248 |
2. If a collector, recurse into it. |
| 249 |
|
| 250 |
5. ``pytest_collectreport(report)`` |
| 251 |
|
| 252 |
2. ``pytest_collection_modifyitems(session, config, items)`` |
| 253 |
|
| 254 |
1. ``pytest_deselected(items)`` for any deselected items (may be called multiple times) |
| 255 |
|
| 256 |
3. ``pytest_collection_finish(session)`` |
| 257 |
4. Set ``session.items`` to the list of collected items |
| 258 |
5. Set ``session.testscollected`` to the number of collected items |
| 259 |
|
| 260 |
You can implement this hook to only perform some action before collection, |
| 261 |
for example the terminal plugin uses it to start displaying the collection |
| 262 |
counter (and returns `None`). |
| 263 |
|
| 264 |
:param session: The pytest session object. |
| 265 |
|
| 266 |
Use in conftest plugins |
| 267 |
======================= |
| 268 |
|
| 269 |
This hook is only called for :ref:`initial conftests <pluginorder>`. |
| 270 |
""" |
| 271 |
|
| 272 |
|
| 273 |
def pytest_collection_modifyitems( |
| 274 |
session: "Session", config: "Config", items: List["Item"] |
| 275 |
) -> None: |
| 276 |
"""Called after collection has been performed. May filter or re-order |
| 277 |
the items in-place. |
| 278 |
|
| 279 |
:param session: The pytest session object. |
| 280 |
:param config: The pytest config object. |
| 281 |
:param items: List of item objects. |
| 282 |
|
| 283 |
Use in conftest plugins |
| 284 |
======================= |
| 285 |
|
| 286 |
Any conftest plugin can implement this hook. |
| 287 |
""" |
| 288 |
|
| 289 |
|
| 290 |
def pytest_collection_finish(session: "Session") -> None: |
| 291 |
"""Called after collection has been performed and modified. |
| 292 |
|
| 293 |
:param session: The pytest session object. |
| 294 |
|
| 295 |
Use in conftest plugins |
| 296 |
======================= |
| 297 |
|
| 298 |
Any conftest plugin can implement this hook. |
| 299 |
""" |
| 300 |
|
| 301 |
|
| 302 |
@hookspec( |
| 303 |
firstresult=True, |
| 304 |
warn_on_impl_args={ |
| 305 |
"path": HOOK_LEGACY_PATH_ARG.format( |
| 306 |
pylib_path_arg="path", pathlib_path_arg="collection_path" |
| 307 |
), |
| 308 |
}, |
| 309 |
) |
| 310 |
def pytest_ignore_collect( |
| 311 |
collection_path: Path, path: "LEGACY_PATH", config: "Config" |
| 312 |
) -> Optional[bool]: |
| 313 |
"""Return True to prevent considering this path for collection. |
| 314 |
|
| 315 |
This hook is consulted for all files and directories prior to calling |
| 316 |
more specific hooks. |
| 317 |
|
| 318 |
Stops at first non-None result, see :ref:`firstresult`. |
| 319 |
|
| 320 |
:param collection_path: The path to analyze. |
| 321 |
:param path: The path to analyze (deprecated). |
| 322 |
:param config: The pytest config object. |
| 323 |
|
| 324 |
.. versionchanged:: 7.0.0 |
| 325 |
The ``collection_path`` parameter was added as a :class:`pathlib.Path` |
| 326 |
equivalent of the ``path`` parameter. The ``path`` parameter |
| 327 |
has been deprecated. |
| 328 |
|
| 329 |
Use in conftest plugins |
| 330 |
======================= |
| 331 |
|
| 332 |
Any conftest file can implement this hook. For a given collection path, only |
| 333 |
conftest files in parent directories of the collection path are consulted |
| 334 |
(if the path is a directory, its own conftest file is *not* consulted - a |
| 335 |
directory cannot ignore itself!). |
| 336 |
""" |
| 337 |
|
| 338 |
|
| 339 |
@hookspec(firstresult=True) |
| 340 |
def pytest_collect_directory(path: Path, parent: "Collector") -> "Optional[Collector]": |
| 341 |
"""Create a :class:`~pytest.Collector` for the given directory, or None if |
| 342 |
not relevant. |
| 343 |
|
| 344 |
.. versionadded:: 8.0 |
| 345 |
|
| 346 |
For best results, the returned collector should be a subclass of |
| 347 |
:class:`~pytest.Directory`, but this is not required. |
| 348 |
|
| 349 |
The new node needs to have the specified ``parent`` as a parent. |
| 350 |
|
| 351 |
Stops at first non-None result, see :ref:`firstresult`. |
| 352 |
|
| 353 |
:param path: The path to analyze. |
| 354 |
|
| 355 |
See :ref:`custom directory collectors` for a simple example of use of this |
| 356 |
hook. |
| 357 |
|
| 358 |
Use in conftest plugins |
| 359 |
======================= |
| 360 |
|
| 361 |
Any conftest file can implement this hook. For a given collection path, only |
| 362 |
conftest files in parent directories of the collection path are consulted |
| 363 |
(if the path is a directory, its own conftest file is *not* consulted - a |
| 364 |
directory cannot collect itself!). |
| 365 |
""" |
| 366 |
|
| 367 |
|
| 368 |
@hookspec( |
| 369 |
warn_on_impl_args={ |
| 370 |
"path": HOOK_LEGACY_PATH_ARG.format( |
| 371 |
pylib_path_arg="path", pathlib_path_arg="file_path" |
| 372 |
), |
| 373 |
}, |
| 374 |
) |
| 375 |
def pytest_collect_file( |
| 376 |
file_path: Path, path: "LEGACY_PATH", parent: "Collector" |
| 377 |
) -> "Optional[Collector]": |
| 378 |
"""Create a :class:`~pytest.Collector` for the given path, or None if not relevant. |
| 379 |
|
| 380 |
For best results, the returned collector should be a subclass of |
| 381 |
:class:`~pytest.File`, but this is not required. |
| 382 |
|
| 383 |
The new node needs to have the specified ``parent`` as a parent. |
| 384 |
|
| 385 |
:param file_path: The path to analyze. |
| 386 |
:param path: The path to collect (deprecated). |
| 387 |
|
| 388 |
.. versionchanged:: 7.0.0 |
| 389 |
The ``file_path`` parameter was added as a :class:`pathlib.Path` |
| 390 |
equivalent of the ``path`` parameter. The ``path`` parameter |
| 391 |
has been deprecated. |
| 392 |
|
| 393 |
Use in conftest plugins |
| 394 |
======================= |
| 395 |
|
| 396 |
Any conftest file can implement this hook. For a given file path, only |
| 397 |
conftest files in parent directories of the file path are consulted. |
| 398 |
""" |
| 399 |
|
| 400 |
|
| 401 |
# logging hooks for collection |
| 402 |
|
| 403 |
|
| 404 |
def pytest_collectstart(collector: "Collector") -> None: |
| 405 |
"""Collector starts collecting. |
| 406 |
|
| 407 |
:param collector: |
| 408 |
The collector. |
| 409 |
|
| 410 |
Use in conftest plugins |
| 411 |
======================= |
| 412 |
|
| 413 |
Any conftest file can implement this hook. For a given collector, only |
| 414 |
conftest files in the collector's directory and its parent directories are |
| 415 |
consulted. |
| 416 |
""" |
| 417 |
|
| 418 |
|
| 419 |
def pytest_itemcollected(item: "Item") -> None: |
| 420 |
"""We just collected a test item. |
| 421 |
|
| 422 |
:param item: |
| 423 |
The item. |
| 424 |
|
| 425 |
Use in conftest plugins |
| 426 |
======================= |
| 427 |
|
| 428 |
Any conftest file can implement this hook. For a given item, only conftest |
| 429 |
files in the item's directory and its parent directories are consulted. |
| 430 |
""" |
| 431 |
|
| 432 |
|
| 433 |
def pytest_collectreport(report: "CollectReport") -> None: |
| 434 |
"""Collector finished collecting. |
| 435 |
|
| 436 |
:param report: |
| 437 |
The collect report. |
| 438 |
|
| 439 |
Use in conftest plugins |
| 440 |
======================= |
| 441 |
|
| 442 |
Any conftest file can implement this hook. For a given collector, only |
| 443 |
conftest files in the collector's directory and its parent directories are |
| 444 |
consulted. |
| 445 |
""" |
| 446 |
|
| 447 |
|
| 448 |
def pytest_deselected(items: Sequence["Item"]) -> None: |
| 449 |
"""Called for deselected test items, e.g. by keyword. |
| 450 |
|
| 451 |
May be called multiple times. |
| 452 |
|
| 453 |
:param items: |
| 454 |
The items. |
| 455 |
|
| 456 |
Use in conftest plugins |
| 457 |
======================= |
| 458 |
|
| 459 |
Any conftest file can implement this hook. |
| 460 |
""" |
| 461 |
|
| 462 |
|
| 463 |
@hookspec(firstresult=True) |
| 464 |
def pytest_make_collect_report(collector: "Collector") -> "Optional[CollectReport]": |
| 465 |
"""Perform :func:`collector.collect() <pytest.Collector.collect>` and return |
| 466 |
a :class:`~pytest.CollectReport`. |
| 467 |
|
| 468 |
Stops at first non-None result, see :ref:`firstresult`. |
| 469 |
|
| 470 |
:param collector: |
| 471 |
The collector. |
| 472 |
|
| 473 |
Use in conftest plugins |
| 474 |
======================= |
| 475 |
|
| 476 |
Any conftest file can implement this hook. For a given collector, only |
| 477 |
conftest files in the collector's directory and its parent directories are |
| 478 |
consulted. |
| 479 |
""" |
| 480 |
|
| 481 |
|
| 482 |
# ------------------------------------------------------------------------- |
| 483 |
# Python test function related hooks |
| 484 |
# ------------------------------------------------------------------------- |
| 485 |
|
| 486 |
|
| 487 |
@hookspec( |
| 488 |
firstresult=True, |
| 489 |
warn_on_impl_args={ |
| 490 |
"path": HOOK_LEGACY_PATH_ARG.format( |
| 491 |
pylib_path_arg="path", pathlib_path_arg="module_path" |
| 492 |
), |
| 493 |
}, |
| 494 |
) |
| 495 |
def pytest_pycollect_makemodule( |
| 496 |
module_path: Path, path: "LEGACY_PATH", parent |
| 497 |
) -> Optional["Module"]: |
| 498 |
"""Return a :class:`pytest.Module` collector or None for the given path. |
| 499 |
|
| 500 |
This hook will be called for each matching test module path. |
| 501 |
The :hook:`pytest_collect_file` hook needs to be used if you want to |
| 502 |
create test modules for files that do not match as a test module. |
| 503 |
|
| 504 |
Stops at first non-None result, see :ref:`firstresult`. |
| 505 |
|
| 506 |
:param module_path: The path of the module to collect. |
| 507 |
:param path: The path of the module to collect (deprecated). |
| 508 |
|
| 509 |
.. versionchanged:: 7.0.0 |
| 510 |
The ``module_path`` parameter was added as a :class:`pathlib.Path` |
| 511 |
equivalent of the ``path`` parameter. |
| 512 |
|
| 513 |
The ``path`` parameter has been deprecated in favor of ``fspath``. |
| 514 |
|
| 515 |
Use in conftest plugins |
| 516 |
======================= |
| 517 |
|
| 518 |
Any conftest file can implement this hook. For a given parent collector, |
| 519 |
only conftest files in the collector's directory and its parent directories |
| 520 |
are consulted. |
| 521 |
""" |
| 522 |
|
| 523 |
|
| 524 |
@hookspec(firstresult=True) |
| 525 |
def pytest_pycollect_makeitem( |
| 526 |
collector: Union["Module", "Class"], name: str, obj: object |
| 527 |
) -> Union[None, "Item", "Collector", List[Union["Item", "Collector"]]]: |
| 528 |
"""Return a custom item/collector for a Python object in a module, or None. |
| 529 |
|
| 530 |
Stops at first non-None result, see :ref:`firstresult`. |
| 531 |
|
| 532 |
:param collector: |
| 533 |
The module/class collector. |
| 534 |
:param name: |
| 535 |
The name of the object in the module/class. |
| 536 |
:param obj: |
| 537 |
The object. |
| 538 |
:returns: |
| 539 |
The created items/collectors. |
| 540 |
|
| 541 |
Use in conftest plugins |
| 542 |
======================= |
| 543 |
|
| 544 |
Any conftest file can implement this hook. For a given collector, only |
| 545 |
conftest files in the collector's directory and its parent directories |
| 546 |
are consulted. |
| 547 |
""" |
| 548 |
|
| 549 |
|
| 550 |
@hookspec(firstresult=True) |
| 551 |
def pytest_pyfunc_call(pyfuncitem: "Function") -> Optional[object]: |
| 552 |
"""Call underlying test function. |
| 553 |
|
| 554 |
Stops at first non-None result, see :ref:`firstresult`. |
| 555 |
|
| 556 |
:param pyfuncitem: |
| 557 |
The function item. |
| 558 |
|
| 559 |
Use in conftest plugins |
| 560 |
======================= |
| 561 |
|
| 562 |
Any conftest file can implement this hook. For a given item, only |
| 563 |
conftest files in the item's directory and its parent directories |
| 564 |
are consulted. |
| 565 |
""" |
| 566 |
|
| 567 |
|
| 568 |
def pytest_generate_tests(metafunc: "Metafunc") -> None: |
| 569 |
"""Generate (multiple) parametrized calls to a test function. |
| 570 |
|
| 571 |
:param metafunc: |
| 572 |
The :class:`~pytest.Metafunc` helper for the test function. |
| 573 |
|
| 574 |
Use in conftest plugins |
| 575 |
======================= |
| 576 |
|
| 577 |
Any conftest file can implement this hook. For a given function definition, |
| 578 |
only conftest files in the functions's directory and its parent directories |
| 579 |
are consulted. |
| 580 |
""" |
| 581 |
|
| 582 |
|
| 583 |
@hookspec(firstresult=True) |
| 584 |
def pytest_make_parametrize_id( |
| 585 |
config: "Config", val: object, argname: str |
| 586 |
) -> Optional[str]: |
| 587 |
"""Return a user-friendly string representation of the given ``val`` |
| 588 |
that will be used by @pytest.mark.parametrize calls, or None if the hook |
| 589 |
doesn't know about ``val``. |
| 590 |
|
| 591 |
The parameter name is available as ``argname``, if required. |
| 592 |
|
| 593 |
Stops at first non-None result, see :ref:`firstresult`. |
| 594 |
|
| 595 |
:param config: The pytest config object. |
| 596 |
:param val: The parametrized value. |
| 597 |
:param argname: The automatic parameter name produced by pytest. |
| 598 |
|
| 599 |
Use in conftest plugins |
| 600 |
======================= |
| 601 |
|
| 602 |
Any conftest file can implement this hook. |
| 603 |
""" |
| 604 |
|
| 605 |
|
| 606 |
# ------------------------------------------------------------------------- |
| 607 |
# runtest related hooks |
| 608 |
# ------------------------------------------------------------------------- |
| 609 |
|
| 610 |
|
| 611 |
@hookspec(firstresult=True) |
| 612 |
def pytest_runtestloop(session: "Session") -> Optional[object]: |
| 613 |
"""Perform the main runtest loop (after collection finished). |
| 614 |
|
| 615 |
The default hook implementation performs the runtest protocol for all items |
| 616 |
collected in the session (``session.items``), unless the collection failed |
| 617 |
or the ``collectonly`` pytest option is set. |
| 618 |
|
| 619 |
If at any point :py:func:`pytest.exit` is called, the loop is |
| 620 |
terminated immediately. |
| 621 |
|
| 622 |
If at any point ``session.shouldfail`` or ``session.shouldstop`` are set, the |
| 623 |
loop is terminated after the runtest protocol for the current item is finished. |
| 624 |
|
| 625 |
:param session: The pytest session object. |
| 626 |
|
| 627 |
Stops at first non-None result, see :ref:`firstresult`. |
| 628 |
The return value is not used, but only stops further processing. |
| 629 |
|
| 630 |
Use in conftest plugins |
| 631 |
======================= |
| 632 |
|
| 633 |
Any conftest file can implement this hook. |
| 634 |
""" |
| 635 |
|
| 636 |
|
| 637 |
@hookspec(firstresult=True) |
| 638 |
def pytest_runtest_protocol( |
| 639 |
item: "Item", nextitem: "Optional[Item]" |
| 640 |
) -> Optional[object]: |
| 641 |
"""Perform the runtest protocol for a single test item. |
| 642 |
|
| 643 |
The default runtest protocol is this (see individual hooks for full details): |
| 644 |
|
| 645 |
- ``pytest_runtest_logstart(nodeid, location)`` |
| 646 |
|
| 647 |
- Setup phase: |
| 648 |
- ``call = pytest_runtest_setup(item)`` (wrapped in ``CallInfo(when="setup")``) |
| 649 |
- ``report = pytest_runtest_makereport(item, call)`` |
| 650 |
- ``pytest_runtest_logreport(report)`` |
| 651 |
- ``pytest_exception_interact(call, report)`` if an interactive exception occurred |
| 652 |
|
| 653 |
- Call phase, if the setup passed and the ``setuponly`` pytest option is not set: |
| 654 |
- ``call = pytest_runtest_call(item)`` (wrapped in ``CallInfo(when="call")``) |
| 655 |
- ``report = pytest_runtest_makereport(item, call)`` |
| 656 |
- ``pytest_runtest_logreport(report)`` |
| 657 |
- ``pytest_exception_interact(call, report)`` if an interactive exception occurred |
| 658 |
|
| 659 |
- Teardown phase: |
| 660 |
- ``call = pytest_runtest_teardown(item, nextitem)`` (wrapped in ``CallInfo(when="teardown")``) |
| 661 |
- ``report = pytest_runtest_makereport(item, call)`` |
| 662 |
- ``pytest_runtest_logreport(report)`` |
| 663 |
- ``pytest_exception_interact(call, report)`` if an interactive exception occurred |
| 664 |
|
| 665 |
- ``pytest_runtest_logfinish(nodeid, location)`` |
| 666 |
|
| 667 |
:param item: Test item for which the runtest protocol is performed. |
| 668 |
:param nextitem: The scheduled-to-be-next test item (or None if this is the end my friend). |
| 669 |
|
| 670 |
Stops at first non-None result, see :ref:`firstresult`. |
| 671 |
The return value is not used, but only stops further processing. |
| 672 |
|
| 673 |
Use in conftest plugins |
| 674 |
======================= |
| 675 |
|
| 676 |
Any conftest file can implement this hook. |
| 677 |
""" |
| 678 |
|
| 679 |
|
| 680 |
def pytest_runtest_logstart( |
| 681 |
nodeid: str, location: Tuple[str, Optional[int], str] |
| 682 |
) -> None: |
| 683 |
"""Called at the start of running the runtest protocol for a single item. |
| 684 |
|
| 685 |
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol. |
| 686 |
|
| 687 |
:param nodeid: Full node ID of the item. |
| 688 |
:param location: A tuple of ``(filename, lineno, testname)`` |
| 689 |
where ``filename`` is a file path relative to ``config.rootpath`` |
| 690 |
and ``lineno`` is 0-based. |
| 691 |
|
| 692 |
Use in conftest plugins |
| 693 |
======================= |
| 694 |
|
| 695 |
Any conftest file can implement this hook. For a given item, only conftest |
| 696 |
files in the item's directory and its parent directories are consulted. |
| 697 |
""" |
| 698 |
|
| 699 |
|
| 700 |
def pytest_runtest_logfinish( |
| 701 |
nodeid: str, location: Tuple[str, Optional[int], str] |
| 702 |
) -> None: |
| 703 |
"""Called at the end of running the runtest protocol for a single item. |
| 704 |
|
| 705 |
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol. |
| 706 |
|
| 707 |
:param nodeid: Full node ID of the item. |
| 708 |
:param location: A tuple of ``(filename, lineno, testname)`` |
| 709 |
where ``filename`` is a file path relative to ``config.rootpath`` |
| 710 |
and ``lineno`` is 0-based. |
| 711 |
|
| 712 |
Use in conftest plugins |
| 713 |
======================= |
| 714 |
|
| 715 |
Any conftest file can implement this hook. For a given item, only conftest |
| 716 |
files in the item's directory and its parent directories are consulted. |
| 717 |
""" |
| 718 |
|
| 719 |
|
| 720 |
def pytest_runtest_setup(item: "Item") -> None: |
| 721 |
"""Called to perform the setup phase for a test item. |
| 722 |
|
| 723 |
The default implementation runs ``setup()`` on ``item`` and all of its |
| 724 |
parents (which haven't been setup yet). This includes obtaining the |
| 725 |
values of fixtures required by the item (which haven't been obtained |
| 726 |
yet). |
| 727 |
|
| 728 |
:param item: |
| 729 |
The item. |
| 730 |
|
| 731 |
Use in conftest plugins |
| 732 |
======================= |
| 733 |
|
| 734 |
Any conftest file can implement this hook. For a given item, only conftest |
| 735 |
files in the item's directory and its parent directories are consulted. |
| 736 |
""" |
| 737 |
|
| 738 |
|
| 739 |
def pytest_runtest_call(item: "Item") -> None: |
| 740 |
"""Called to run the test for test item (the call phase). |
| 741 |
|
| 742 |
The default implementation calls ``item.runtest()``. |
| 743 |
|
| 744 |
:param item: |
| 745 |
The item. |
| 746 |
|
| 747 |
Use in conftest plugins |
| 748 |
======================= |
| 749 |
|
| 750 |
Any conftest file can implement this hook. For a given item, only conftest |
| 751 |
files in the item's directory and its parent directories are consulted. |
| 752 |
""" |
| 753 |
|
| 754 |
|
| 755 |
def pytest_runtest_teardown(item: "Item", nextitem: Optional["Item"]) -> None: |
| 756 |
"""Called to perform the teardown phase for a test item. |
| 757 |
|
| 758 |
The default implementation runs the finalizers and calls ``teardown()`` |
| 759 |
on ``item`` and all of its parents (which need to be torn down). This |
| 760 |
includes running the teardown phase of fixtures required by the item (if |
| 761 |
they go out of scope). |
| 762 |
|
| 763 |
:param item: |
| 764 |
The item. |
| 765 |
:param nextitem: |
| 766 |
The scheduled-to-be-next test item (None if no further test item is |
| 767 |
scheduled). This argument is used to perform exact teardowns, i.e. |
| 768 |
calling just enough finalizers so that nextitem only needs to call |
| 769 |
setup functions. |
| 770 |
|
| 771 |
Use in conftest plugins |
| 772 |
======================= |
| 773 |
|
| 774 |
Any conftest file can implement this hook. For a given item, only conftest |
| 775 |
files in the item's directory and its parent directories are consulted. |
| 776 |
""" |
| 777 |
|
| 778 |
|
| 779 |
@hookspec(firstresult=True) |
| 780 |
def pytest_runtest_makereport( |
| 781 |
item: "Item", call: "CallInfo[None]" |
| 782 |
) -> Optional["TestReport"]: |
| 783 |
"""Called to create a :class:`~pytest.TestReport` for each of |
| 784 |
the setup, call and teardown runtest phases of a test item. |
| 785 |
|
| 786 |
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol. |
| 787 |
|
| 788 |
:param item: The item. |
| 789 |
:param call: The :class:`~pytest.CallInfo` for the phase. |
| 790 |
|
| 791 |
Stops at first non-None result, see :ref:`firstresult`. |
| 792 |
|
| 793 |
Use in conftest plugins |
| 794 |
======================= |
| 795 |
|
| 796 |
Any conftest file can implement this hook. For a given item, only conftest |
| 797 |
files in the item's directory and its parent directories are consulted. |
| 798 |
""" |
| 799 |
|
| 800 |
|
| 801 |
def pytest_runtest_logreport(report: "TestReport") -> None: |
| 802 |
"""Process the :class:`~pytest.TestReport` produced for each |
| 803 |
of the setup, call and teardown runtest phases of an item. |
| 804 |
|
| 805 |
See :hook:`pytest_runtest_protocol` for a description of the runtest protocol. |
| 806 |
|
| 807 |
Use in conftest plugins |
| 808 |
======================= |
| 809 |
|
| 810 |
Any conftest file can implement this hook. For a given item, only conftest |
| 811 |
files in the item's directory and its parent directories are consulted. |
| 812 |
""" |
| 813 |
|
| 814 |
|
| 815 |
@hookspec(firstresult=True) |
| 816 |
def pytest_report_to_serializable( |
| 817 |
config: "Config", |
| 818 |
report: Union["CollectReport", "TestReport"], |
| 819 |
) -> Optional[Dict[str, Any]]: |
| 820 |
"""Serialize the given report object into a data structure suitable for |
| 821 |
sending over the wire, e.g. converted to JSON. |
| 822 |
|
| 823 |
:param config: The pytest config object. |
| 824 |
:param report: The report. |
| 825 |
|
| 826 |
Use in conftest plugins |
| 827 |
======================= |
| 828 |
|
| 829 |
Any conftest file can implement this hook. The exact details may depend |
| 830 |
on the plugin which calls the hook. |
| 831 |
""" |
| 832 |
|
| 833 |
|
| 834 |
@hookspec(firstresult=True) |
| 835 |
def pytest_report_from_serializable( |
| 836 |
config: "Config", |
| 837 |
data: Dict[str, Any], |
| 838 |
) -> Optional[Union["CollectReport", "TestReport"]]: |
| 839 |
"""Restore a report object previously serialized with |
| 840 |
:hook:`pytest_report_to_serializable`. |
| 841 |
|
| 842 |
:param config: The pytest config object. |
| 843 |
|
| 844 |
Use in conftest plugins |
| 845 |
======================= |
| 846 |
|
| 847 |
Any conftest file can implement this hook. The exact details may depend |
| 848 |
on the plugin which calls the hook. |
| 849 |
""" |
| 850 |
|
| 851 |
|
| 852 |
# ------------------------------------------------------------------------- |
| 853 |
# Fixture related hooks |
| 854 |
# ------------------------------------------------------------------------- |
| 855 |
|
| 856 |
|
| 857 |
@hookspec(firstresult=True) |
| 858 |
def pytest_fixture_setup( |
| 859 |
fixturedef: "FixtureDef[Any]", request: "SubRequest" |
| 860 |
) -> Optional[object]: |
| 861 |
"""Perform fixture setup execution. |
| 862 |
|
| 863 |
:param fixturedef: |
| 864 |
The fixture definition object. |
| 865 |
:param request: |
| 866 |
The fixture request object. |
| 867 |
:returns: |
| 868 |
The return value of the call to the fixture function. |
| 869 |
|
| 870 |
Stops at first non-None result, see :ref:`firstresult`. |
| 871 |
|
| 872 |
.. note:: |
| 873 |
If the fixture function returns None, other implementations of |
| 874 |
this hook function will continue to be called, according to the |
| 875 |
behavior of the :ref:`firstresult` option. |
| 876 |
|
| 877 |
Use in conftest plugins |
| 878 |
======================= |
| 879 |
|
| 880 |
Any conftest file can implement this hook. For a given fixture, only |
| 881 |
conftest files in the fixture scope's directory and its parent directories |
| 882 |
are consulted. |
| 883 |
""" |
| 884 |
|
| 885 |
|
| 886 |
def pytest_fixture_post_finalizer( |
| 887 |
fixturedef: "FixtureDef[Any]", request: "SubRequest" |
| 888 |
) -> None: |
| 889 |
"""Called after fixture teardown, but before the cache is cleared, so |
| 890 |
the fixture result ``fixturedef.cached_result`` is still available (not |
| 891 |
``None``). |
| 892 |
|
| 893 |
:param fixturedef: |
| 894 |
The fixture definition object. |
| 895 |
:param request: |
| 896 |
The fixture request object. |
| 897 |
|
| 898 |
Use in conftest plugins |
| 899 |
======================= |
| 900 |
|
| 901 |
Any conftest file can implement this hook. For a given fixture, only |
| 902 |
conftest files in the fixture scope's directory and its parent directories |
| 903 |
are consulted. |
| 904 |
""" |
| 905 |
|
| 906 |
|
| 907 |
# ------------------------------------------------------------------------- |
| 908 |
# test session related hooks |
| 909 |
# ------------------------------------------------------------------------- |
| 910 |
|
| 911 |
|
| 912 |
def pytest_sessionstart(session: "Session") -> None: |
| 913 |
"""Called after the ``Session`` object has been created and before performing collection |
| 914 |
and entering the run test loop. |
| 915 |
|
| 916 |
:param session: The pytest session object. |
| 917 |
|
| 918 |
Use in conftest plugins |
| 919 |
======================= |
| 920 |
|
| 921 |
This hook is only called for :ref:`initial conftests <pluginorder>`. |
| 922 |
""" |
| 923 |
|
| 924 |
|
| 925 |
def pytest_sessionfinish( |
| 926 |
session: "Session", |
| 927 |
exitstatus: Union[int, "ExitCode"], |
| 928 |
) -> None: |
| 929 |
"""Called after whole test run finished, right before returning the exit status to the system. |
| 930 |
|
| 931 |
:param session: The pytest session object. |
| 932 |
:param exitstatus: The status which pytest will return to the system. |
| 933 |
|
| 934 |
Use in conftest plugins |
| 935 |
======================= |
| 936 |
|
| 937 |
Any conftest file can implement this hook. |
| 938 |
""" |
| 939 |
|
| 940 |
|
| 941 |
def pytest_unconfigure(config: "Config") -> None: |
| 942 |
"""Called before test process is exited. |
| 943 |
|
| 944 |
:param config: The pytest config object. |
| 945 |
|
| 946 |
Use in conftest plugins |
| 947 |
======================= |
| 948 |
|
| 949 |
Any conftest file can implement this hook. |
| 950 |
""" |
| 951 |
|
| 952 |
|
| 953 |
# ------------------------------------------------------------------------- |
| 954 |
# hooks for customizing the assert methods |
| 955 |
# ------------------------------------------------------------------------- |
| 956 |
|
| 957 |
|
| 958 |
def pytest_assertrepr_compare( |
| 959 |
config: "Config", op: str, left: object, right: object |
| 960 |
) -> Optional[List[str]]: |
| 961 |
"""Return explanation for comparisons in failing assert expressions. |
| 962 |
|
| 963 |
Return None for no custom explanation, otherwise return a list |
| 964 |
of strings. The strings will be joined by newlines but any newlines |
| 965 |
*in* a string will be escaped. Note that all but the first line will |
| 966 |
be indented slightly, the intention is for the first line to be a summary. |
| 967 |
|
| 968 |
:param config: The pytest config object. |
| 969 |
:param op: The operator, e.g. `"=="`, `"!="`, `"not in"`. |
| 970 |
:param left: The left operand. |
| 971 |
:param right: The right operand. |
| 972 |
|
| 973 |
Use in conftest plugins |
| 974 |
======================= |
| 975 |
|
| 976 |
Any conftest file can implement this hook. For a given item, only conftest |
| 977 |
files in the item's directory and its parent directories are consulted. |
| 978 |
""" |
| 979 |
|
| 980 |
|
| 981 |
def pytest_assertion_pass(item: "Item", lineno: int, orig: str, expl: str) -> None: |
| 982 |
"""Called whenever an assertion passes. |
| 983 |
|
| 984 |
.. versionadded:: 5.0 |
| 985 |
|
| 986 |
Use this hook to do some processing after a passing assertion. |
| 987 |
The original assertion information is available in the `orig` string |
| 988 |
and the pytest introspected assertion information is available in the |
| 989 |
`expl` string. |
| 990 |
|
| 991 |
This hook must be explicitly enabled by the ``enable_assertion_pass_hook`` |
| 992 |
ini-file option: |
| 993 |
|
| 994 |
.. code-block:: ini |
| 995 |
|
| 996 |
[pytest] |
| 997 |
enable_assertion_pass_hook=true |
| 998 |
|
| 999 |
You need to **clean the .pyc** files in your project directory and interpreter libraries |
| 1000 |
when enabling this option, as assertions will require to be re-written. |
| 1001 |
|
| 1002 |
:param item: pytest item object of current test. |
| 1003 |
:param lineno: Line number of the assert statement. |
| 1004 |
:param orig: String with the original assertion. |
| 1005 |
:param expl: String with the assert explanation. |
| 1006 |
|
| 1007 |
Use in conftest plugins |
| 1008 |
======================= |
| 1009 |
|
| 1010 |
Any conftest file can implement this hook. For a given item, only conftest |
| 1011 |
files in the item's directory and its parent directories are consulted. |
| 1012 |
""" |
| 1013 |
|
| 1014 |
|
| 1015 |
# ------------------------------------------------------------------------- |
| 1016 |
# Hooks for influencing reporting (invoked from _pytest_terminal). |
| 1017 |
# ------------------------------------------------------------------------- |
| 1018 |
|
| 1019 |
|
| 1020 |
@hookspec( |
| 1021 |
warn_on_impl_args={ |
| 1022 |
"startdir": HOOK_LEGACY_PATH_ARG.format( |
| 1023 |
pylib_path_arg="startdir", pathlib_path_arg="start_path" |
| 1024 |
), |
| 1025 |
}, |
| 1026 |
) |
| 1027 |
def pytest_report_header( # type:ignore[empty-body] |
| 1028 |
config: "Config", start_path: Path, startdir: "LEGACY_PATH" |
| 1029 |
) -> Union[str, List[str]]: |
| 1030 |
"""Return a string or list of strings to be displayed as header info for terminal reporting. |
| 1031 |
|
| 1032 |
:param config: The pytest config object. |
| 1033 |
:param start_path: The starting dir. |
| 1034 |
:param startdir: The starting dir (deprecated). |
| 1035 |
|
| 1036 |
.. note:: |
| 1037 |
|
| 1038 |
Lines returned by a plugin are displayed before those of plugins which |
| 1039 |
ran before it. |
| 1040 |
If you want to have your line(s) displayed first, use |
| 1041 |
:ref:`trylast=True <plugin-hookorder>`. |
| 1042 |
|
| 1043 |
.. versionchanged:: 7.0.0 |
| 1044 |
The ``start_path`` parameter was added as a :class:`pathlib.Path` |
| 1045 |
equivalent of the ``startdir`` parameter. The ``startdir`` parameter |
| 1046 |
has been deprecated. |
| 1047 |
|
| 1048 |
Use in conftest plugins |
| 1049 |
======================= |
| 1050 |
|
| 1051 |
This hook is only called for :ref:`initial conftests <pluginorder>`. |
| 1052 |
""" |
| 1053 |
|
| 1054 |
|
| 1055 |
@hookspec( |
| 1056 |
warn_on_impl_args={ |
| 1057 |
"startdir": HOOK_LEGACY_PATH_ARG.format( |
| 1058 |
pylib_path_arg="startdir", pathlib_path_arg="start_path" |
| 1059 |
), |
| 1060 |
}, |
| 1061 |
) |
| 1062 |
def pytest_report_collectionfinish( # type:ignore[empty-body] |
| 1063 |
config: "Config", |
| 1064 |
start_path: Path, |
| 1065 |
startdir: "LEGACY_PATH", |
| 1066 |
items: Sequence["Item"], |
| 1067 |
) -> Union[str, List[str]]: |
| 1068 |
"""Return a string or list of strings to be displayed after collection |
| 1069 |
has finished successfully. |
| 1070 |
|
| 1071 |
These strings will be displayed after the standard "collected X items" message. |
| 1072 |
|
| 1073 |
.. versionadded:: 3.2 |
| 1074 |
|
| 1075 |
:param config: The pytest config object. |
| 1076 |
:param start_path: The starting dir. |
| 1077 |
:param startdir: The starting dir (deprecated). |
| 1078 |
:param items: List of pytest items that are going to be executed; this list should not be modified. |
| 1079 |
|
| 1080 |
.. note:: |
| 1081 |
|
| 1082 |
Lines returned by a plugin are displayed before those of plugins which |
| 1083 |
ran before it. |
| 1084 |
If you want to have your line(s) displayed first, use |
| 1085 |
:ref:`trylast=True <plugin-hookorder>`. |
| 1086 |
|
| 1087 |
.. versionchanged:: 7.0.0 |
| 1088 |
The ``start_path`` parameter was added as a :class:`pathlib.Path` |
| 1089 |
equivalent of the ``startdir`` parameter. The ``startdir`` parameter |
| 1090 |
has been deprecated. |
| 1091 |
|
| 1092 |
Use in conftest plugins |
| 1093 |
======================= |
| 1094 |
|
| 1095 |
Any conftest plugin can implement this hook. |
| 1096 |
""" |
| 1097 |
|
| 1098 |
|
| 1099 |
@hookspec(firstresult=True) |
| 1100 |
def pytest_report_teststatus( # type:ignore[empty-body] |
| 1101 |
report: Union["CollectReport", "TestReport"], config: "Config" |
| 1102 |
) -> "TestShortLogReport | Tuple[str, str, Union[str, Tuple[str, Mapping[str, bool]]]]": |
| 1103 |
"""Return result-category, shortletter and verbose word for status |
| 1104 |
reporting. |
| 1105 |
|
| 1106 |
The result-category is a category in which to count the result, for |
| 1107 |
example "passed", "skipped", "error" or the empty string. |
| 1108 |
|
| 1109 |
The shortletter is shown as testing progresses, for example ".", "s", |
| 1110 |
"E" or the empty string. |
| 1111 |
|
| 1112 |
The verbose word is shown as testing progresses in verbose mode, for |
| 1113 |
example "PASSED", "SKIPPED", "ERROR" or the empty string. |
| 1114 |
|
| 1115 |
pytest may style these implicitly according to the report outcome. |
| 1116 |
To provide explicit styling, return a tuple for the verbose word, |
| 1117 |
for example ``"rerun", "R", ("RERUN", {"yellow": True})``. |
| 1118 |
|
| 1119 |
:param report: The report object whose status is to be returned. |
| 1120 |
:param config: The pytest config object. |
| 1121 |
:returns: The test status. |
| 1122 |
|
| 1123 |
Stops at first non-None result, see :ref:`firstresult`. |
| 1124 |
|
| 1125 |
Use in conftest plugins |
| 1126 |
======================= |
| 1127 |
|
| 1128 |
Any conftest plugin can implement this hook. |
| 1129 |
""" |
| 1130 |
|
| 1131 |
|
| 1132 |
def pytest_terminal_summary( |
| 1133 |
terminalreporter: "TerminalReporter", |
| 1134 |
exitstatus: "ExitCode", |
| 1135 |
config: "Config", |
| 1136 |
) -> None: |
| 1137 |
"""Add a section to terminal summary reporting. |
| 1138 |
|
| 1139 |
:param terminalreporter: The internal terminal reporter object. |
| 1140 |
:param exitstatus: The exit status that will be reported back to the OS. |
| 1141 |
:param config: The pytest config object. |
| 1142 |
|
| 1143 |
.. versionadded:: 4.2 |
| 1144 |
The ``config`` parameter. |
| 1145 |
|
| 1146 |
Use in conftest plugins |
| 1147 |
======================= |
| 1148 |
|
| 1149 |
Any conftest plugin can implement this hook. |
| 1150 |
""" |
| 1151 |
|
| 1152 |
|
| 1153 |
@hookspec(historic=True) |
| 1154 |
def pytest_warning_recorded( |
| 1155 |
warning_message: "warnings.WarningMessage", |
| 1156 |
when: "Literal['config', 'collect', 'runtest']", |
| 1157 |
nodeid: str, |
| 1158 |
location: Optional[Tuple[str, int, str]], |
| 1159 |
) -> None: |
| 1160 |
"""Process a warning captured by the internal pytest warnings plugin. |
| 1161 |
|
| 1162 |
:param warning_message: |
| 1163 |
The captured warning. This is the same object produced by :class:`warnings.catch_warnings`, |
| 1164 |
and contains the same attributes as the parameters of :py:func:`warnings.showwarning`. |
| 1165 |
|
| 1166 |
:param when: |
| 1167 |
Indicates when the warning was captured. Possible values: |
| 1168 |
|
| 1169 |
* ``"config"``: during pytest configuration/initialization stage. |
| 1170 |
* ``"collect"``: during test collection. |
| 1171 |
* ``"runtest"``: during test execution. |
| 1172 |
|
| 1173 |
:param nodeid: |
| 1174 |
Full id of the item. Empty string for warnings that are not specific to |
| 1175 |
a particular node. |
| 1176 |
|
| 1177 |
:param location: |
| 1178 |
When available, holds information about the execution context of the captured |
| 1179 |
warning (filename, linenumber, function). ``function`` evaluates to <module> |
| 1180 |
when the execution context is at the module level. |
| 1181 |
|
| 1182 |
.. versionadded:: 6.0 |
| 1183 |
|
| 1184 |
Use in conftest plugins |
| 1185 |
======================= |
| 1186 |
|
| 1187 |
Any conftest file can implement this hook. If the warning is specific to a |
| 1188 |
particular node, only conftest files in parent directories of the node are |
| 1189 |
consulted. |
| 1190 |
""" |
| 1191 |
|
| 1192 |
|
| 1193 |
# ------------------------------------------------------------------------- |
| 1194 |
# Hooks for influencing skipping |
| 1195 |
# ------------------------------------------------------------------------- |
| 1196 |
|
| 1197 |
|
| 1198 |
def pytest_markeval_namespace( # type:ignore[empty-body] |
| 1199 |
config: "Config", |
| 1200 |
) -> Dict[str, Any]: |
| 1201 |
"""Called when constructing the globals dictionary used for |
| 1202 |
evaluating string conditions in xfail/skipif markers. |
| 1203 |
|
| 1204 |
This is useful when the condition for a marker requires |
| 1205 |
objects that are expensive or impossible to obtain during |
| 1206 |
collection time, which is required by normal boolean |
| 1207 |
conditions. |
| 1208 |
|
| 1209 |
.. versionadded:: 6.2 |
| 1210 |
|
| 1211 |
:param config: The pytest config object. |
| 1212 |
:returns: A dictionary of additional globals to add. |
| 1213 |
|
| 1214 |
Use in conftest plugins |
| 1215 |
======================= |
| 1216 |
|
| 1217 |
Any conftest file can implement this hook. For a given item, only conftest |
| 1218 |
files in parent directories of the item are consulted. |
| 1219 |
""" |
| 1220 |
|
| 1221 |
|
| 1222 |
# ------------------------------------------------------------------------- |
| 1223 |
# error handling and internal debugging hooks |
| 1224 |
# ------------------------------------------------------------------------- |
| 1225 |
|
| 1226 |
|
| 1227 |
def pytest_internalerror( |
| 1228 |
excrepr: "ExceptionRepr", |
| 1229 |
excinfo: "ExceptionInfo[BaseException]", |
| 1230 |
) -> Optional[bool]: |
| 1231 |
"""Called for internal errors. |
| 1232 |
|
| 1233 |
Return True to suppress the fallback handling of printing an |
| 1234 |
INTERNALERROR message directly to sys.stderr. |
| 1235 |
|
| 1236 |
:param excrepr: The exception repr object. |
| 1237 |
:param excinfo: The exception info. |
| 1238 |
|
| 1239 |
Use in conftest plugins |
| 1240 |
======================= |
| 1241 |
|
| 1242 |
Any conftest plugin can implement this hook. |
| 1243 |
""" |
| 1244 |
|
| 1245 |
|
| 1246 |
def pytest_keyboard_interrupt( |
| 1247 |
excinfo: "ExceptionInfo[Union[KeyboardInterrupt, Exit]]", |
| 1248 |
) -> None: |
| 1249 |
"""Called for keyboard interrupt. |
| 1250 |
|
| 1251 |
:param excinfo: The exception info. |
| 1252 |
|
| 1253 |
Use in conftest plugins |
| 1254 |
======================= |
| 1255 |
|
| 1256 |
Any conftest plugin can implement this hook. |
| 1257 |
""" |
| 1258 |
|
| 1259 |
|
| 1260 |
def pytest_exception_interact( |
| 1261 |
node: Union["Item", "Collector"], |
| 1262 |
call: "CallInfo[Any]", |
| 1263 |
report: Union["CollectReport", "TestReport"], |
| 1264 |
) -> None: |
| 1265 |
"""Called when an exception was raised which can potentially be |
| 1266 |
interactively handled. |
| 1267 |
|
| 1268 |
May be called during collection (see :hook:`pytest_make_collect_report`), |
| 1269 |
in which case ``report`` is a :class:`~pytest.CollectReport`. |
| 1270 |
|
| 1271 |
May be called during runtest of an item (see :hook:`pytest_runtest_protocol`), |
| 1272 |
in which case ``report`` is a :class:`~pytest.TestReport`. |
| 1273 |
|
| 1274 |
This hook is not called if the exception that was raised is an internal |
| 1275 |
exception like ``skip.Exception``. |
| 1276 |
|
| 1277 |
:param node: |
| 1278 |
The item or collector. |
| 1279 |
:param call: |
| 1280 |
The call information. Contains the exception. |
| 1281 |
:param report: |
| 1282 |
The collection or test report. |
| 1283 |
|
| 1284 |
Use in conftest plugins |
| 1285 |
======================= |
| 1286 |
|
| 1287 |
Any conftest file can implement this hook. For a given node, only conftest |
| 1288 |
files in parent directories of the node are consulted. |
| 1289 |
""" |
| 1290 |
|
| 1291 |
|
| 1292 |
def pytest_enter_pdb(config: "Config", pdb: "pdb.Pdb") -> None: |
| 1293 |
"""Called upon pdb.set_trace(). |
| 1294 |
|
| 1295 |
Can be used by plugins to take special action just before the python |
| 1296 |
debugger enters interactive mode. |
| 1297 |
|
| 1298 |
:param config: The pytest config object. |
| 1299 |
:param pdb: The Pdb instance. |
| 1300 |
|
| 1301 |
Use in conftest plugins |
| 1302 |
======================= |
| 1303 |
|
| 1304 |
Any conftest plugin can implement this hook. |
| 1305 |
""" |
| 1306 |
|
| 1307 |
|
| 1308 |
def pytest_leave_pdb(config: "Config", pdb: "pdb.Pdb") -> None: |
| 1309 |
"""Called when leaving pdb (e.g. with continue after pdb.set_trace()). |
| 1310 |
|
| 1311 |
Can be used by plugins to take special action just after the python |
| 1312 |
debugger leaves interactive mode. |
| 1313 |
|
| 1314 |
:param config: The pytest config object. |
| 1315 |
:param pdb: The Pdb instance. |
| 1316 |
|
| 1317 |
Use in conftest plugins |
| 1318 |
======================= |
| 1319 |
|
| 1320 |
Any conftest plugin can implement this hook. |
| 1321 |
""" |