diff options
author | Pedro Algarvio <pedro@algarvio.me> | 2020-08-27 17:52:16 +0100 |
---|---|---|
committer | Ran Benita <ran@unusedvar.com> | 2020-12-12 17:41:37 +0200 |
commit | b16c0912537bee06c83e202112f4b036e4fd66dc (patch) | |
tree | dddb8336bebd669a6009472722a7cf4442b65607 /src | |
parent | 902739cfc3bbc3379e6ef99c8e250de35f52ecde (diff) | |
download | pytest-b16c0912537bee06c83e202112f4b036e4fd66dc.tar.gz |
Add `pytest_markeval_namespace` hook.
Add a new hook , `pytest_markeval_namespace` which should return a dictionary.
This dictionary will be used to augment the "global" variables available to evaluate skipif/xfail/xpass markers.
Pseudo example
``conftest.py``:
.. code-block:: python
def pytest_markeval_namespace():
return {"color": "red"}
``test_func.py``:
.. code-block:: python
@pytest.mark.skipif("color == 'blue'", reason="Color is not red")
def test_func():
assert False
Diffstat (limited to 'src')
-rw-r--r-- | src/_pytest/hookspec.py | 21 | ||||
-rw-r--r-- | src/_pytest/skipping.py | 11 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/_pytest/hookspec.py b/src/_pytest/hookspec.py index 33ca782cf..e499b742c 100644 --- a/src/_pytest/hookspec.py +++ b/src/_pytest/hookspec.py @@ -809,6 +809,27 @@ def pytest_warning_recorded( # ------------------------------------------------------------------------- +# Hooks for influencing skipping +# ------------------------------------------------------------------------- + + +def pytest_markeval_namespace(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 _pytest.config.Config config: The pytest config object. + :returns: A dictionary of additional globals to add. + """ + + +# ------------------------------------------------------------------------- # error handling and internal debugging hooks # ------------------------------------------------------------------------- diff --git a/src/_pytest/skipping.py b/src/_pytest/skipping.py index afc3610eb..9aacfecee 100644 --- a/src/_pytest/skipping.py +++ b/src/_pytest/skipping.py @@ -3,6 +3,7 @@ import os import platform import sys import traceback +from collections.abc import Mapping from typing import Generator from typing import Optional from typing import Tuple @@ -98,6 +99,16 @@ def evaluate_condition(item: Item, mark: Mark, condition: object) -> Tuple[bool, "platform": platform, "config": item.config, } + for dictionary in reversed( + item.ihook.pytest_markeval_namespace(config=item.config) + ): + if not isinstance(dictionary, Mapping): + raise ValueError( + "pytest_markeval_namespace() needs to return a dict, got {!r}".format( + dictionary + ) + ) + globals_.update(dictionary) if hasattr(item, "obj"): globals_.update(item.obj.__globals__) # type: ignore[attr-defined] try: |