diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 77d8b52ca46..1ac95b388ab 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -935,13 +935,11 @@ def collect(self) -> Iterator[nodes.Item | nodes.Collector]: is_match = node.path == matchparts[0] if sys.platform == "win32" and not is_match: # In case the file paths do not match, fallback to samefile() to - # account for short-paths on Windows (#11895). - same_file = os.path.samefile(node.path, matchparts[0]) - # We don't want to match links to the current node, - # otherwise we would match the same file more than once (#12039). - is_match = same_file and ( - os.path.islink(node.path) - == os.path.islink(matchparts[0]) + # account for short-paths on Windows (#11895). But use a version + # which doesn't resolve symlinks, otherwise we might match the + # same file more than once (#12039). + is_match = os.path.samestat( + node.path.lstat(), matchparts[0].lstat() ) # Name part e.g. `TestIt` in `/a/b/test_file.py::TestIt::test_it`.