| 1 | diff --git a/src/_pytest/nodes.py b/src/_pytest/nodes.py |
| 2 | --- a/src/_pytest/nodes.py |
| 3 | +++ b/src/_pytest/nodes.py |
| 4 | @@ -29,6 +29,7 @@ |
| 5 | from _pytest.mark.structures import MarkDecorator |
| 6 | from _pytest.mark.structures import NodeKeywords |
| 7 | from _pytest.outcomes import fail |
| 8 | +from _pytest.pathlib import Path |
| 9 | from _pytest.store import Store |
| 10 | |
| 11 | if TYPE_CHECKING: |
| 12 | @@ -361,9 +362,14 @@ def _repr_failure_py( |
| 13 | else: |
| 14 | truncate_locals = True |
| 15 | |
| 16 | + # excinfo.getrepr() formats paths relative to the CWD if `abspath` is False. |
| 17 | + # It is possible for a fixture/test to change the CWD while this code runs, which |
| 18 | + # would then result in the user seeing confusing paths in the failure message. |
| 19 | + # To fix this, if the CWD changed, always display the full absolute path. |
| 20 | + # It will be better to just always display paths relative to invocation_dir, but |
| 21 | + # this requires a lot of plumbing (#6428). |
| 22 | try: |
| 23 | - os.getcwd() |
| 24 | - abspath = False |
| 25 | + abspath = Path(os.getcwd()) != Path(self.config.invocation_dir) |
| 26 | except OSError: |
| 27 | abspath = True |
| 28 | |