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