Skip to content

pytest.mark.xfail from inside the test is ignored #12649

@tsyg

Description

@tsyg

When pytest.mark.xfail is used inside a test - it is not causing the test to be marked as xfail.
The sample code:

import pytest

@pytest.mark.parametrize("way", [
    "fresh",  # OK
    pytest.param("mix", marks=pytest.mark.xfail(reason="known_issue_with_MIX", strict=True)),
    "mixed",
])
def test_demo(way):
    print(f"testing - {way}")
    if way.startswith("mix"):
        pytest.mark.xfail(reason="mix has known issue!")  # Here - is ignored?
        print("NOTE: Marked sa xfail - see the result..")
    assert way.startswith("fr"), "only 'fr*' way is OK"

Expected result:
"fresh" test passed, "mix" and "mixed" ones treated as known issue (xfail)

Actual result: "mixed" test is shown failed.

> pip list
Package            Version
------------------ ----------
AllPairs           2.0.1
allpairspy         2.5.1
certifi            2023.11.17
charset-normalizer 3.3.2
colorama           0.4.6
idna               3.6
iniconfig          2.0.0
packaging          23.2
pip                23.3.1
pluggy             1.3.0
pyserial           3.5
pytest             7.4.3
requests           2.31.0
setuptools         69.0.2
urllib3            2.1.0
wheel              0.41.3

got the same in another venv:
pytest: 8.1.1
Python: 3.12.2
Platform: Windows-11-10.0.22631-SP0
pluggy: 1.5.0
Plugins:

  • html: 4.1.1
  • metadata: 3.1.1

Screenshot 2024-07-22 192401

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: marksrelated to marks, either the general marks or builtintype: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions