pytest_robotframework.hooks

new hooks defined by the pytest_robotframework plugin. these are not to be imported. see the documentation for pytest hook functions for information on how to use them.

 1# ideally this would be in _internal.pytest so this would go without saying, but i can't figure out
 2# any other way to get this module to show up in pdoc
 3"""new hooks defined by the pytest_robotframework plugin. these are not to be imported. see
 4[the documentation for pytest hook functions](https://docs.pytest.org/en/7.1.x/how-to/writing_hook_functions.html)
 5for information on how to use them."""
 6
 7from __future__ import annotations
 8
 9from typing import TYPE_CHECKING
10
11from pytest import Item, hookspec
12
13if TYPE_CHECKING:
14    from pytest import Session
15
16    from pytest_robotframework._internal.robot.utils import RobotOptions
17
18# these are basically abstract methods, but hooks are defined in this wacky way which isn't
19# supported by linters/type checkers:
20
21# https://github.com/pytest-dev/pytest/issues/11300
22# pyright:reportReturnType=false
23# https://github.com/astral-sh/ruff/issues/7286
24# https://github.com/astral-sh/ruff/issues/9803
25# ruff: noqa: ARG001, FBT001, PLR0917
26
27
28@hookspec
29def pytest_robot_modify_options(options: RobotOptions, session: Session) -> None:
30    """modify the arguments passed to robot in-place
31
32    :param options: the arguments to be passed to robot in dict format. for example,
33    `{"listener": ["Foo", "Bar"]}`means `--listener Foo --listener Bar`). you can also specify
34    instances of classes to `listener` and `prerebotmodifier`
35    :param session: the pytest `Session` object
36
37    example:
38    -------
39    >>> def pytest_robot_modify_options(options: RobotOptions, session: Session) -> None:
40    ... if not session.config.option.collectonly:
41    ...     options["loglevel"] = "DEBUG:INFO"
42    ...     options["listener"].append(Foo())
43    """
44
45
46@hookspec
47def pytest_robot_assertion(
48    item: Item,
49    expression: str,
50    fail_message: object,
51    line_number: int,
52    assertion_error: AssertionError | None,
53    explanation: str,
54) -> None:
55    """gets called when an assertion runs. unlike `pytest_assertrepr_compare` and
56    `pytest_assertion_pass`, this hook is executed on both passing and failing assertions, and
57    allows you to see the second argument passed to `assert` statement
58
59    requires the `enable_assertion_pass_hook` pytest option to be enabled
60
61    :param item:
62        the currently running item
63    :param expression:
64        a string containing the the source code of the expression passed to the `assert` statement
65    :param fail_message:
66        the second argument to the `assert` statement, or `None` if there was none provided
67    :param line_number:
68        the line number containing the `assert` statement
69    :param assertion_error:
70        the exception raised if the `assert` statement failed. `None` if the assertion passed.
71        you must re-raise the assertion error for the assertion to fail (useful if you want to
72        conditionally ignore an assertion error)
73    :param explanation:
74        pytest's explanation of the result. the format will be different depending on whether the
75        assertion passed or failed
76
77    warning:
78    -------
79    this hook is experimental and relies heavily on patching the internals of pytest. it may break,
80    change or be removed at any time. you should only use this hook if you know what you're doing
81    """
@hookspec
def pytest_robot_modify_options( options: pytest_robotframework._internal.robot.utils.RobotOptions, session: _pytest.main.Session) -> None:
29@hookspec
30def pytest_robot_modify_options(options: RobotOptions, session: Session) -> None:
31    """modify the arguments passed to robot in-place
32
33    :param options: the arguments to be passed to robot in dict format. for example,
34    `{"listener": ["Foo", "Bar"]}`means `--listener Foo --listener Bar`). you can also specify
35    instances of classes to `listener` and `prerebotmodifier`
36    :param session: the pytest `Session` object
37
38    example:
39    -------
40    >>> def pytest_robot_modify_options(options: RobotOptions, session: Session) -> None:
41    ... if not session.config.option.collectonly:
42    ...     options["loglevel"] = "DEBUG:INFO"
43    ...     options["listener"].append(Foo())
44    """

modify the arguments passed to robot in-place

Parameters
  • options: the arguments to be passed to robot in dict format. for example, {"listener": ["Foo", "Bar"]}means --listener Foo --listener Bar). you can also specify instances of classes to listener and prerebotmodifier
  • session: the pytest Session object

example:

>>> def pytest_robot_modify_options(options: RobotOptions, session: Session) -> None:
... if not session.config.option.collectonly:
...     options["loglevel"] = "DEBUG:INFO"
...     options["listener"].append(Foo())
@hookspec
def pytest_robot_assertion( item: _pytest.nodes.Item, expression: str, fail_message: object, line_number: int, assertion_error: AssertionError | None, explanation: str) -> None:
47@hookspec
48def pytest_robot_assertion(
49    item: Item,
50    expression: str,
51    fail_message: object,
52    line_number: int,
53    assertion_error: AssertionError | None,
54    explanation: str,
55) -> None:
56    """gets called when an assertion runs. unlike `pytest_assertrepr_compare` and
57    `pytest_assertion_pass`, this hook is executed on both passing and failing assertions, and
58    allows you to see the second argument passed to `assert` statement
59
60    requires the `enable_assertion_pass_hook` pytest option to be enabled
61
62    :param item:
63        the currently running item
64    :param expression:
65        a string containing the the source code of the expression passed to the `assert` statement
66    :param fail_message:
67        the second argument to the `assert` statement, or `None` if there was none provided
68    :param line_number:
69        the line number containing the `assert` statement
70    :param assertion_error:
71        the exception raised if the `assert` statement failed. `None` if the assertion passed.
72        you must re-raise the assertion error for the assertion to fail (useful if you want to
73        conditionally ignore an assertion error)
74    :param explanation:
75        pytest's explanation of the result. the format will be different depending on whether the
76        assertion passed or failed
77
78    warning:
79    -------
80    this hook is experimental and relies heavily on patching the internals of pytest. it may break,
81    change or be removed at any time. you should only use this hook if you know what you're doing
82    """

gets called when an assertion runs. unlike pytest_assertrepr_compare and pytest_assertion_pass, this hook is executed on both passing and failing assertions, and allows you to see the second argument passed to assert statement

requires the enable_assertion_pass_hook pytest option to be enabled

Parameters
  • item: the currently running item
  • expression: a string containing the the source code of the expression passed to the assert statement
  • fail_message: the second argument to the assert statement, or None if there was none provided
  • line_number: the line number containing the assert statement
  • assertion_error: the exception raised if the assert statement failed. None if the assertion passed. you must re-raise the assertion error for the assertion to fail (useful if you want to conditionally ignore an assertion error)
  • explanation: pytest's explanation of the result. the format will be different depending on whether the assertion passed or failed

warning:

this hook is experimental and relies heavily on patching the internals of pytest. it may break, change or be removed at any time. you should only use this hook if you know what you're doing