diff --git a/changelog/1335.bugfix.rst b/changelog/1335.bugfix.rst new file mode 100644 index 00000000..063ecd68 --- /dev/null +++ b/changelog/1335.bugfix.rst @@ -0,0 +1 @@ +``--dist=loadscope`` no longer splits a module or class into several work units when a parametrization id contains ``::`` (for example IPv6 addresses). diff --git a/src/xdist/scheduler/loadscope.py b/src/xdist/scheduler/loadscope.py index 69099d99..2bc2ee31 100644 --- a/src/xdist/scheduler/loadscope.py +++ b/src/xdist/scheduler/loadscope.py @@ -314,16 +314,25 @@ def _split_scope(self, nodeid: str) -> str: #. Method of a class in a test module. #. Doctest in a function in a package. - This function will group tests with the scope determined by splitting - the first ``::`` from the right. That is, classes will be grouped in a + This function will group tests with the scope determined by dropping + the last name of the nodeid. That is, classes will be grouped in a single work unit, and functions from a test module will be grouped by their module. In the above example, scopes will be:: example/loadsuite/test/test_beta.py example/loadsuite/test/test_delta.py::Delta1 example/loadsuite/epsilon/__init__.py + + The nodeid is parsed the same way pytest does: the first ``::`` ends + the path and the first ``[`` after it starts the parametrization id, + so ``::`` or brackets inside a parametrization id (e.g. IPv6 + addresses) or ``[`` in the path do not change the scope. """ - return nodeid.rsplit("::", 1)[0] + path, sep, tail = nodeid.partition("::") + if not sep: + return nodeid + names = tail.partition("[")[0].split("::") + return "::".join([path, *names[:-1]]) def _pending_of(self, workload: dict[str, dict[str, bool]]) -> int: """Return the number of pending tests in a workload.""" diff --git a/testing/acceptance_test.py b/testing/acceptance_test.py index 8fe47de9..a5a67a00 100644 --- a/testing/acceptance_test.py +++ b/testing/acceptance_test.py @@ -8,6 +8,7 @@ import pytest import xdist +from xdist.scheduler.loadscope import LoadScopeScheduling class TestDistribution: @@ -1338,6 +1339,45 @@ def pytest_collection_modifyitems(): class TestLoadScope: + @pytest.mark.parametrize( + ("nodeid", "expected"), + [ + ("a.py::test", "a.py"), + ("a.py::TestA::test", "a.py::TestA"), + ("a.py::TestA::TestB::test", "a.py::TestA::TestB"), + ("pkg/__init__.py::pkg.func", "pkg/__init__.py"), + ("a.txt", "a.txt"), + ("a.py::test[::1-x]", "a.py"), + ("a.py::TestA::test[cafe:cafe::cafe]", "a.py::TestA"), + ("a.yaml::item[::1]", "a.yaml"), + ("a.py::test[x]::y]", "a.py"), + ("a.py::test[[::1]:80]", "a.py"), + ("dir[/a.py::test[::1]", "dir[/a.py"), + ("test[1].py::test[::1]", "test[1].py"), + ], + ) + def test_split_scope(self, nodeid: str, expected: str) -> None: + sched = LoadScopeScheduling.__new__(LoadScopeScheduling) + assert sched._split_scope(nodeid) == expected + + def test_by_module_with_double_colons_in_ids( + self, pytester: pytest.Pytester + ) -> None: + test_file = """ + import pytest + @pytest.mark.parametrize('i', ['::1', 'cafe:cafe::cafe', 'x]::y', 'x']) + def test(i): + pass + """ + pytester.makepyfile(test_a=test_file, test_b=test_file) + result = pytester.runpytest("-n2", "--dist=loadscope", "-v") + assert get_workers_and_test_count_by_prefix( + "test_a.py::test", result.outlines + ) in ({"gw0": 4}, {"gw1": 4}) + assert get_workers_and_test_count_by_prefix( + "test_b.py::test", result.outlines + ) in ({"gw0": 4}, {"gw1": 4}) + def test_by_module(self, pytester: pytest.Pytester) -> None: test_file = """ import pytest