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