Skip to content

Hooks

pytest_robotframework._internal.pytest.hooks

new pytest 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.

pytest_robot_assertion(item, expression, fail_message, line_number, assertion_error, explanation)

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

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

Parameters:

Name Type Description Default
item Item

the currently running item

required
expression str

a string containing the the source code of the expression passed to the assert statement

required
fail_message object

the second argument to the assert statement, or None if there was none provided

required
line_number int

the line number containing the assert statement

required
assertion_error AssertionError | None

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)

required
explanation str

pytest's explanation of the result. the format will be different depending on whether the assertion passed or failed

required
Source code in pytest_robotframework/_internal/pytest/hooks.py
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
@hookspec
def pytest_robot_assertion(
    item: Item,
    expression: str,
    fail_message: object,
    line_number: int,
    assertion_error: AssertionError | None,
    explanation: str,
) -> None:
    """
    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

    !!! 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

    :param item:
        the currently running item
    :param expression:
        a string containing the the source code of the expression passed to the `assert` statement
    :param fail_message:
        the second argument to the `assert` statement, or `None` if there was none provided
    :param line_number:
        the line number containing the `assert` statement
    :param 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)
    :param explanation:
        pytest's explanation of the result. the format will be different depending on whether the
        assertion passed or failed
    """

pytest_robot_modify_options(options, session)

modify the arguments passed to robot in-place

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())

Parameters:

Name Type Description Default
options RobotOptions

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

required
session Session

the pytest Session object

required
Source code in pytest_robotframework/_internal/pytest/hooks.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@hookspec
def pytest_robot_modify_options(options: RobotOptions, session: Session) -> None:
    """
    modify the arguments passed to robot in-place

    example:
    -------
    ```py
    def pytest_robot_modify_options(options: RobotOptions, session: Session) -> None:
    if not session.config.option.collectonly:
        options["loglevel"] = "DEBUG:INFO"
        options["listener"].append(Foo())
    ```

    :param 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`
    :param session: the pytest `Session` object
    """