pytest-dev / pytest-dev/pytest

skipping: possible improvements to pytest.mark.skipif/xfail condition handling

Open
#7,395 8 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: marks type: proposal
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, sys and platform Python 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.
  • 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:

  1. The condition string can be used as the default reason string, so supplying a reason is not required.
  2. 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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.