diff --git a/src/_pytest/main.py b/src/_pytest/main.py index 2a8b06fce..24e184a0f 100644 --- a/src/_pytest/main.py +++ b/src/_pytest/main.py @@ -886,7 +886,8 @@ def resolve_collection_argument( parts[-1] = f"{parts[-1]}{squacket}{rest}" if as_pypath: strpath = search_pypath(strpath) - fspath = absolutepath(invocation_path / strpath) + fspath = invocation_path / strpath + fspath = absolutepath(fspath) if not fspath.exists(): msg = ( "module or package not found: {arg} (missing __init__.py?)" diff --git a/testing/test_main.py b/testing/test_main.py index 7a40b8262..fa5fde7ec 100644 --- a/testing/test_main.py +++ b/testing/test_main.py @@ -180,6 +180,10 @@ class TestResolveCollectionArgument: invocation_path / "src/pkg", [], ) + assert resolve_collection_argument(invocation_path, "src/pkg/test.py::") == ( + invocation_path / "src/pkg/test.py", + [], + ) with pytest.raises( UsageError, match=r"package argument cannot contain :: selection parts" @@ -207,6 +211,12 @@ class TestResolveCollectionArgument: ): resolve_collection_argument(invocation_path, "src/pkg/test.py[a::b]") + with pytest.raises( + UsageError, + match=re.escape("file or directory not found: src/pkg/test.py[foobar]"), + ): + resolve_collection_argument(invocation_path, "src/pkg/test.py[foobar]") + with pytest.raises( UsageError, match=re.escape(