| 1 |
""" hook specifications for pytest plugins, invoked from main.py and builtin plugins. """ |
| 2 |
|
| 3 |
from _pytest._pluggy import HookspecMarker |
| 4 |
|
| 5 |
hookspec = HookspecMarker("pytest") |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
@hookspec(historic=True) |
| 12 |
def pytest_addhooks(pluginmanager): |
| 13 |
"""called at plugin registration time to allow adding new hooks via a call to |
| 14 |
pluginmanager.add_hookspecs(module_or_class, prefix).""" |
| 15 |
|
| 16 |
|
| 17 |
@hookspec(historic=True) |
| 18 |
def pytest_namespace(): |
| 19 |
"""return dict of name->object to be made globally available in |
| 20 |
the pytest namespace. This hook is called at plugin registration |
| 21 |
time. |
| 22 |
""" |
| 23 |
|
| 24 |
@hookspec(historic=True) |
| 25 |
def pytest_plugin_registered(plugin, manager): |
| 26 |
""" a new pytest plugin got registered. """ |
| 27 |
|
| 28 |
|
| 29 |
@hookspec(historic=True) |
| 30 |
def pytest_addoption(parser): |
| 31 |
"""register argparse-style options and ini-style config values, |
| 32 |
called once at the beginning of a test run. |
| 33 |
|
| 34 |
.. note:: |
| 35 |
|
| 36 |
This function should be implemented only in plugins or ``conftest.py`` |
| 37 |
files situated at the tests root directory due to how py.test |
| 38 |
:ref:`discovers plugins during startup <pluginorder>`. |
| 39 |
|
| 40 |
:arg parser: To add command line options, call |
| 41 |
:py:func:`parser.addoption(...) <_pytest.config.Parser.addoption>`. |
| 42 |
To add ini-file values call :py:func:`parser.addini(...) |
| 43 |
<_pytest.config.Parser.addini>`. |
| 44 |
|
| 45 |
Options can later be accessed through the |
| 46 |
:py:class:`config <_pytest.config.Config>` object, respectively: |
| 47 |
|
| 48 |
- :py:func:`config.getoption(name) <_pytest.config.Config.getoption>` to |
| 49 |
retrieve the value of a command line option. |
| 50 |
|
| 51 |
- :py:func:`config.getini(name) <_pytest.config.Config.getini>` to retrieve |
| 52 |
a value read from an ini-style file. |
| 53 |
|
| 54 |
The config object is passed around on many internal objects via the ``.config`` |
| 55 |
attribute or can be retrieved as the ``pytestconfig`` fixture or accessed |
| 56 |
via (deprecated) ``pytest.config``. |
| 57 |
""" |
| 58 |
|
| 59 |
@hookspec(historic=True) |
| 60 |
def pytest_configure(config): |
| 61 |
""" called after command line options have been parsed |
| 62 |
and all plugins and initial conftest files been loaded. |
| 63 |
This hook is called for every plugin. |
| 64 |
""" |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
@hookspec(firstresult=True) |
| 73 |
def pytest_cmdline_parse(pluginmanager, args): |
| 74 |
"""return initialized config object, parsing the specified args. """ |
| 75 |
|
| 76 |
def pytest_cmdline_preparse(config, args): |
| 77 |
"""(deprecated) modify command line arguments before option parsing. """ |
| 78 |
|
| 79 |
@hookspec(firstresult=True) |
| 80 |
def pytest_cmdline_main(config): |
| 81 |
""" called for performing the main command line action. The default |
| 82 |
implementation will invoke the configure hooks and runtest_mainloop. """ |
| 83 |
|
| 84 |
def pytest_load_initial_conftests(early_config, parser, args): |
| 85 |
""" implements the loading of initial conftest files ahead |
| 86 |
of command line option parsing. """ |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
@hookspec(firstresult=True) |
| 94 |
def pytest_collection(session): |
| 95 |
""" perform the collection protocol for the given session. """ |
| 96 |
|
| 97 |
def pytest_collection_modifyitems(session, config, items): |
| 98 |
""" called after collection has been performed, may filter or re-order |
| 99 |
the items in-place.""" |
| 100 |
|
| 101 |
def pytest_collection_finish(session): |
| 102 |
""" called after collection has been performed and modified. """ |
| 103 |
|
| 104 |
@hookspec(firstresult=True) |
| 105 |
def pytest_ignore_collect(path, config): |
| 106 |
""" return True to prevent considering this path for collection. |
| 107 |
This hook is consulted for all files and directories prior to calling |
| 108 |
more specific hooks. |
| 109 |
""" |
| 110 |
|
| 111 |
@hookspec(firstresult=True) |
| 112 |
def pytest_collect_directory(path, parent): |
| 113 |
""" called before traversing a directory for collection files. """ |
| 114 |
|
| 115 |
def pytest_collect_file(path, parent): |
| 116 |
""" return collection Node or None for the given path. Any new node |
| 117 |
needs to have the specified ``parent`` as a parent.""" |
| 118 |
|
| 119 |
|
| 120 |
def pytest_collectstart(collector): |
| 121 |
""" collector starts collecting. """ |
| 122 |
|
| 123 |
def pytest_itemcollected(item): |
| 124 |
""" we just collected a test item. """ |
| 125 |
|
| 126 |
def pytest_collectreport(report): |
| 127 |
""" collector finished collecting. """ |
| 128 |
|
| 129 |
def pytest_deselected(items): |
| 130 |
""" called for test items deselected by keyword. """ |
| 131 |
|
| 132 |
@hookspec(firstresult=True) |
| 133 |
def pytest_make_collect_report(collector): |
| 134 |
""" perform ``collector.collect()`` and return a CollectReport. """ |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
@hookspec(firstresult=True) |
| 141 |
def pytest_pycollect_makemodule(path, parent): |
| 142 |
""" return a Module collector or None for the given path. |
| 143 |
This hook will be called for each matching test module path. |
| 144 |
The pytest_collect_file hook needs to be used if you want to |
| 145 |
create test modules for files that do not match as a test module. |
| 146 |
""" |
| 147 |
|
| 148 |
@hookspec(firstresult=True) |
| 149 |
def pytest_pycollect_makeitem(collector, name, obj): |
| 150 |
""" return custom item/collector for a python object in a module, or None. """ |
| 151 |
|
| 152 |
@hookspec(firstresult=True) |
| 153 |
def pytest_pyfunc_call(pyfuncitem): |
| 154 |
""" call underlying test function. """ |
| 155 |
|
| 156 |
def pytest_generate_tests(metafunc): |
| 157 |
""" generate (multiple) parametrized calls to a test function.""" |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
@hookspec(firstresult=True) |
| 164 |
def pytest_runtestloop(session): |
| 165 |
""" called for performing the main runtest loop |
| 166 |
(after collection finished). """ |
| 167 |
|
| 168 |
def pytest_itemstart(item, node): |
| 169 |
""" (deprecated, use pytest_runtest_logstart). """ |
| 170 |
|
| 171 |
@hookspec(firstresult=True) |
| 172 |
def pytest_runtest_protocol(item, nextitem): |
| 173 |
""" implements the runtest_setup/call/teardown protocol for |
| 174 |
the given test item, including capturing exceptions and calling |
| 175 |
reporting hooks. |
| 176 |
|
| 177 |
:arg item: test item for which the runtest protocol is performed. |
| 178 |
|
| 179 |
:arg nextitem: the scheduled-to-be-next test item (or None if this |
| 180 |
is the end my friend). This argument is passed on to |
| 181 |
:py:func:`pytest_runtest_teardown`. |
| 182 |
|
| 183 |
:return boolean: True if no further hook implementations should be invoked. |
| 184 |
""" |
| 185 |
|
| 186 |
def pytest_runtest_logstart(nodeid, location): |
| 187 |
""" signal the start of running a single test item. """ |
| 188 |
|
| 189 |
def pytest_runtest_setup(item): |
| 190 |
""" called before ``pytest_runtest_call(item)``. """ |
| 191 |
|
| 192 |
def pytest_runtest_call(item): |
| 193 |
""" called to execute the test ``item``. """ |
| 194 |
|
| 195 |
def pytest_runtest_teardown(item, nextitem): |
| 196 |
""" called after ``pytest_runtest_call``. |
| 197 |
|
| 198 |
:arg nextitem: the scheduled-to-be-next test item (None if no further |
| 199 |
test item is scheduled). This argument can be used to |
| 200 |
perform exact teardowns, i.e. calling just enough finalizers |
| 201 |
so that nextitem only needs to call setup-functions. |
| 202 |
""" |
| 203 |
|
| 204 |
@hookspec(firstresult=True) |
| 205 |
def pytest_runtest_makereport(item, call): |
| 206 |
""" return a :py:class:`_pytest.runner.TestReport` object |
| 207 |
for the given :py:class:`pytest.Item` and |
| 208 |
:py:class:`_pytest.runner.CallInfo`. |
| 209 |
""" |
| 210 |
|
| 211 |
def pytest_runtest_logreport(report): |
| 212 |
""" process a test setup/call/teardown report relating to |
| 213 |
the respective phase of executing a test. """ |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
def pytest_sessionstart(session): |
| 220 |
""" before session.main() is called. """ |
| 221 |
|
| 222 |
def pytest_sessionfinish(session, exitstatus): |
| 223 |
""" whole test run finishes. """ |
| 224 |
|
| 225 |
def pytest_unconfigure(config): |
| 226 |
""" called before test process is exited. """ |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
def pytest_assertrepr_compare(config, op, left, right): |
| 234 |
"""return explanation for comparisons in failing assert expressions. |
| 235 |
|
| 236 |
Return None for no custom explanation, otherwise return a list |
| 237 |
of strings. The strings will be joined by newlines but any newlines |
| 238 |
*in* a string will be escaped. Note that all but the first line will |
| 239 |
be indented sligthly, the intention is for the first line to be a summary. |
| 240 |
""" |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
def pytest_report_header(config, startdir): |
| 247 |
""" return a string to be displayed as header info for terminal reporting.""" |
| 248 |
|
| 249 |
@hookspec(firstresult=True) |
| 250 |
def pytest_report_teststatus(report): |
| 251 |
""" return result-category, shortletter and verbose word for reporting.""" |
| 252 |
|
| 253 |
def pytest_terminal_summary(terminalreporter): |
| 254 |
""" add additional section in terminal summary reporting. """ |
| 255 |
|
| 256 |
|
| 257 |
@hookspec(historic=True) |
| 258 |
def pytest_logwarning(message, code, nodeid, fslocation): |
| 259 |
""" process a warning specified by a message, a code string, |
| 260 |
a nodeid and fslocation (both of which may be None |
| 261 |
if the warning is not tied to a partilar node/location).""" |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
@hookspec(firstresult=True) |
| 268 |
def pytest_doctest_prepare_content(content): |
| 269 |
""" return processed content for a given doctest""" |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
def pytest_internalerror(excrepr, excinfo): |
| 276 |
""" called for internal errors. """ |
| 277 |
|
| 278 |
def pytest_keyboard_interrupt(excinfo): |
| 279 |
""" called for keyboard interrupt. """ |
| 280 |
|
| 281 |
def pytest_exception_interact(node, call, report): |
| 282 |
"""called when an exception was raised which can potentially be |
| 283 |
interactively handled. |
| 284 |
|
| 285 |
This hook is only called if an exception was raised |
| 286 |
that is not an internal exception like ``skip.Exception``. |
| 287 |
""" |
| 288 |
|
| 289 |
def pytest_enter_pdb(config): |
| 290 |
""" called upon pdb.set_trace(), can be used by plugins to take special |
| 291 |
action just before the python debugger enters in interactive mode. |
| 292 |
|
| 293 |
:arg config: pytest config object |
| 294 |
:type config: _pytest.config.Config |
| 295 |
""" |