Skip to content

Commit 3d64ca0

Browse files
committed
Allow contibuting additional global variables for skipif/xfail
1 parent 2fcf763 commit 3d64ca0

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/_pytest/hookspec.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,16 @@ def pytest_warning_recorded(
808808
"""
809809

810810

811+
# -------------------------------------------------------------------------
812+
# Hooks for influencing skipping
813+
# -------------------------------------------------------------------------
814+
def pytest_skipif_additional_globals() -> Dict[str, Any]:
815+
"""Called when constructing the globals dictionary used for
816+
evaluating skipif contditions.
817+
Return a dictionary of additional globals to add.
818+
"""
819+
820+
811821
# -------------------------------------------------------------------------
812822
# error handling and internal debugging hooks
813823
# -------------------------------------------------------------------------

src/_pytest/skipping.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def evaluate_condition(item: Item, mark: Mark, condition: object) -> Tuple[bool,
101101
"platform": platform,
102102
"config": item.config,
103103
}
104+
for dictionary in item.config.hook.pytest_skipif_additional_globals():
105+
if not isinstance(dictionary, dict) or not dictionary:
106+
continue
107+
globals_.update(dictionary)
104108
if hasattr(item, "obj"):
105109
globals_.update(item.obj.__globals__) # type: ignore[attr-defined]
106110
try:

testing/test_skipping.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,33 @@ def test_func(self):
153153
assert skipped
154154
assert skipped.reason == "condition: config._hackxyz"
155155

156+
def test_skipif_additional_globals(self, testdir):
157+
testdir.makeconftest(
158+
"""
159+
import pytest
160+
161+
def pytest_skipif_additional_globals():
162+
return {"color": "green"}
163+
"""
164+
)
165+
p = testdir.makepyfile(
166+
"""
167+
import pytest
168+
169+
@pytest.mark.skipif("color == 'green'")
170+
def test_1():
171+
assert True
172+
173+
@pytest.mark.skipif("color == 'red'")
174+
def test_2():
175+
assert True
176+
"""
177+
)
178+
res = testdir.runpytest(p)
179+
assert res.ret == 0
180+
res.stdout.fnmatch_lines(["*1 skipped*"])
181+
res.stdout.fnmatch_lines(["*1 passed*"])
182+
156183

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

0 commit comments

Comments
 (0)