Defer xfail condition evaluation with xfail_if_raises context manager#2153
Conversation
The @pytest.mark.xfail decorator with raises=... evaluates its condition during test collection, which can trigger external calls (e.g., Git().config()) that have side effects. Introduce xfail_if_raises as a context manager that delays this evaluation until test execution time, and apply it to test_index_mutation.
46daafe to
03baced
Compare
|
I didn't change anything that should have affected this one failed test on Cygwin. I have a Windows VM now so I'll see if I can repro. |
|
The failed Cygwin test looks like a known flake with submodule ownership on Cygwin CI that I've been working on and hope to have a PR in for soon. To the best of my knowledge, that is independent of the changes here, though I emphasize that I have not reviewed this PR. I've triggered the test to rerun--my guess is that it will pass. |
I debated a YAGNI approach, but I figured as a utility method, I should try to mirror the decorator as closely as I could. |

Summary
This PR introduces
xfail_if_raises, a context manager that approximates@pytest.mark.xfail(..., raises=...)behavior inside a test body rather than as a decorator.Problem
@pytest.mark.xfailevaluates itsconditionargument during test collection (module import time). Intest_index_mutation, this condition includesGit().config("core.symlinks") == "true", which:git configprocess even when the test is not being runSolution
xfail_if_raisesmoves the xfail logic into the test body. The side-effectful condition is only evaluated if the test actually raises the specified exception(s). This keeps test collection lightweight and avoids relying on in-repo code as part of module import/test discovery.This change is applied to
test_index_mutationintest/test_index.py.AI disclosure
This PR description was prepared with assistance from an agent. All code was written by hand.