Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/1335.bugfix.rst
Original file line number Diff line number Diff line change
@@ -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).
15 changes: 12 additions & 3 deletions src/xdist/scheduler/loadscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down
40 changes: 40 additions & 0 deletions testing/acceptance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import pytest

import xdist
from xdist.scheduler.loadscope import LoadScopeScheduling


class TestDistribution:
Expand Down Expand Up @@ -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
Expand Down