Skip to content

Commit

Permalink
Allow contibuting additional global variables for skipif/xfail
Browse files Browse the repository at this point in the history
  • Loading branch information
s0undt3ch committed Aug 27, 2020
1 parent 2fcf763 commit 3d64ca0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/_pytest/hookspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,16 @@ def pytest_warning_recorded(
"""


# -------------------------------------------------------------------------
# Hooks for influencing skipping
# -------------------------------------------------------------------------
def pytest_skipif_additional_globals() -> Dict[str, Any]:
"""Called when constructing the globals dictionary used for
evaluating skipif contditions.
Return a dictionary of additional globals to add.
"""


# -------------------------------------------------------------------------
# error handling and internal debugging hooks
# -------------------------------------------------------------------------
Expand Down
4 changes: 4 additions & 0 deletions src/_pytest/skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def evaluate_condition(item: Item, mark: Mark, condition: object) -> Tuple[bool,
"platform": platform,
"config": item.config,
}
for dictionary in item.config.hook.pytest_skipif_additional_globals():
if not isinstance(dictionary, dict) or not dictionary:
continue
globals_.update(dictionary)
if hasattr(item, "obj"):
globals_.update(item.obj.__globals__) # type: ignore[attr-defined]
try:
Expand Down
27 changes: 27 additions & 0 deletions testing/test_skipping.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,33 @@ def test_func(self):
assert skipped
assert skipped.reason == "condition: config._hackxyz"

def test_skipif_additional_globals(self, testdir):
testdir.makeconftest(
"""
import pytest
def pytest_skipif_additional_globals():
return {"color": "green"}
"""
)
p = testdir.makepyfile(
"""
import pytest
@pytest.mark.skipif("color == 'green'")
def test_1():
assert True
@pytest.mark.skipif("color == 'red'")
def test_2():
assert True
"""
)
res = testdir.runpytest(p)
assert res.ret == 0
res.stdout.fnmatch_lines(["*1 skipped*"])
res.stdout.fnmatch_lines(["*1 passed*"])


class TestXFail:
@pytest.mark.parametrize("strict", [True, False])
Expand Down

0 comments on commit 3d64ca0

Please sign in to comment.