| 1 | diff --git a/src/_pytest/skipping.py b/src/_pytest/skipping.py |
| 2 | --- a/src/_pytest/skipping.py |
| 3 | +++ b/src/_pytest/skipping.py |
| 4 | @@ -231,17 +231,14 @@ def evaluate_xfail_marks(item: Item) -> Optional[Xfail]: |
| 5 | |
| 6 | @hookimpl(tryfirst=True) |
| 7 | def pytest_runtest_setup(item: Item) -> None: |
| 8 | - item._store[skipped_by_mark_key] = False |
| 9 | - |
| 10 | skipped = evaluate_skip_marks(item) |
| 11 | + item._store[skipped_by_mark_key] = skipped is not None |
| 12 | if skipped: |
| 13 | - item._store[skipped_by_mark_key] = True |
| 14 | skip(skipped.reason) |
| 15 | |
| 16 | - if not item.config.option.runxfail: |
| 17 | - item._store[xfailed_key] = xfailed = evaluate_xfail_marks(item) |
| 18 | - if xfailed and not xfailed.run: |
| 19 | - xfail("[NOTRUN] " + xfailed.reason) |
| 20 | + item._store[xfailed_key] = xfailed = evaluate_xfail_marks(item) |
| 21 | + if xfailed and not item.config.option.runxfail and not xfailed.run: |
| 22 | + xfail("[NOTRUN] " + xfailed.reason) |
| 23 | |
| 24 | |
| 25 | @hookimpl(hookwrapper=True) |
| 26 | @@ -250,12 +247,16 @@ def pytest_runtest_call(item: Item) -> Generator[None, None, None]: |
| 27 | if xfailed is None: |
| 28 | item._store[xfailed_key] = xfailed = evaluate_xfail_marks(item) |
| 29 | |
| 30 | - if not item.config.option.runxfail: |
| 31 | - if xfailed and not xfailed.run: |
| 32 | - xfail("[NOTRUN] " + xfailed.reason) |
| 33 | + if xfailed and not item.config.option.runxfail and not xfailed.run: |
| 34 | + xfail("[NOTRUN] " + xfailed.reason) |
| 35 | |
| 36 | yield |
| 37 | |
| 38 | + # The test run may have added an xfail mark dynamically. |
| 39 | + xfailed = item._store.get(xfailed_key, None) |
| 40 | + if xfailed is None: |
| 41 | + item._store[xfailed_key] = xfailed = evaluate_xfail_marks(item) |
| 42 | + |
| 43 | |
| 44 | @hookimpl(hookwrapper=True) |
| 45 | def pytest_runtest_makereport(item: Item, call: CallInfo[None]): |