pytest-dev / pytest-dev/pytest
skipping: possible improvements to pytest.mark.skipif/xfail condition handling
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 14.5k
- Forks
- 3.4k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 35
Description
The @pytest.mark.skipif and @pytest.mark.xfail can be made conditional by passing conditions into them (usually just one), e.g.
import pytest
import sys
@pytest.mark.xfail(condition=sys.platform == "win32", reason="shouldn't work on Windows")
def test_expected_failure():
assert False # ...
pytest supports two flavors of conditions:
-
A string - in this case the string is
eval'd with an environment consisting of:- The
os,sysandplatformPython modules - The pytest
config - If the Item is a Python Item, the underlying object's
__globals__-- usually its the globals of the module containing the item.
- The
-
A simple boolean, like in the example above.
IMO, the string conditions are inferior in all respects compared to boolean conditions, except for two things:
- The condition string can be used as the default reason string, so supplying a
reasonis not required. - It has access to the
config. A boolean condition is usually evaluated during import time (by normal Python rules), so doesn't have access to it.
In #7388 @RonnyPfannschmidt suggested a possible fix for (2): allow the condition to be a function/lambda, which takes config, e.g.
import pytest
@pytest.mark.xfail(condition=lambda config: config.getoption("foo", False), reason="")
def test_expected_failure():
assert False # ...
This doesn't fix (1), but that's not too bad anyway.
I think with this feature, string conditions become redundant and can be deprecated (though we perhaps wouldn't want to actually deprecate as that would cause a lot of churn).
An alternative is to pass item instead of config. This makes it possible to conditionalize on item properties, and the config is still accessible through item.config.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The issue names no files or tests; begin by locating the existing skipif/xfail condition handling and read the discussion in #7388. Compare the proposed config- and item-based callable designs, with completion requiring an agreed approach for conditional handling and its compatibility with current string and boolean conditions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- testing-qa
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 30/100