Skip to content

Commit 52a3958

Browse files
committed
Create new dist option 'loadgroup'
1 parent 6d83034 commit 52a3958

File tree

6 files changed

+203
-1
lines changed

6 files changed

+203
-1
lines changed

src/xdist/dsession.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
LoadScheduling,
88
LoadScopeScheduling,
99
LoadFileScheduling,
10+
LoadGroupScheduling,
1011
)
1112

1213

@@ -100,6 +101,7 @@ def pytest_xdist_make_scheduler(self, config, log):
100101
"load": LoadScheduling,
101102
"loadscope": LoadScopeScheduling,
102103
"loadfile": LoadFileScheduling,
104+
"loadgroup": LoadGroupScheduling,
103105
}
104106
return schedulers[dist](config, log)
105107

src/xdist/plugin.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def pytest_addoption(parser):
8686
"--dist",
8787
metavar="distmode",
8888
action="store",
89-
choices=["each", "load", "loadscope", "loadfile", "no"],
89+
choices=["each", "load", "loadscope", "loadfile", "loadgroup", "no"],
9090
dest="dist",
9191
default="no",
9292
help=(
@@ -98,6 +98,8 @@ def pytest_addoption(parser):
9898
" the same scope to any available environment.\n\n"
9999
"loadfile: load balance by sending test grouped by file"
100100
" to any available environment.\n\n"
101+
"loadgroup: load balance by sending any pending test or test group"
102+
" to any available enviroment.\n\n"
101103
"(default) no: run tests inprocess, don't distribute."
102104
),
103105
)
@@ -204,6 +206,12 @@ def pytest_configure(config):
204206
config.issue_config_time_warning(warning, 2)
205207
config.option.forked = True
206208

209+
config_line = (
210+
"xgroup: specify group for tests should run in same session."
211+
"in relation to one another. " + "Provided by pytest-xdist."
212+
)
213+
config.addinivalue_line("markers", config_line)
214+
207215

208216
@pytest.hookimpl(tryfirst=True)
209217
def pytest_cmdline_main(config):

src/xdist/remote.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,20 @@ def run_one_test(self, torun):
116116
"runtest_protocol_complete", item_index=self.item_index, duration=duration
117117
)
118118

119+
def pytest_collection_modifyitems(self, session, config, items):
120+
# add the group name to nodeid as suffix if --dist=loadgroup
121+
if config.getvalue("loadgroup"):
122+
for item in items:
123+
try:
124+
mark = item.get_closest_marker("xgroup")
125+
except AttributeError:
126+
mark = item.get_marker("xgroup")
127+
128+
if mark:
129+
gname = mark.kwargs.get("name")
130+
if gname:
131+
item._nodeid = "{}@{}".format(item.nodeid, gname)
132+
119133
@pytest.hookimpl
120134
def pytest_collection_finish(self, session):
121135
try:
@@ -236,6 +250,7 @@ def remote_initconfig(option_dict, args):
236250

237251

238252
def setup_config(config, basetemp):
253+
config.option.loadgroup = True if config.getvalue("dist") == "loadgroup" else False
239254
config.option.looponfail = False
240255
config.option.usepdb = False
241256
config.option.dist = "no"

src/xdist/scheduler/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
from xdist.scheduler.load import LoadScheduling # noqa
33
from xdist.scheduler.loadfile import LoadFileScheduling # noqa
44
from xdist.scheduler.loadscope import LoadScopeScheduling # noqa
5+
from xdist.scheduler.loadgroup import LoadGroupScheduling # noqa

src/xdist/scheduler/loadgroup.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
from .loadscope import LoadScopeScheduling
2+
from py.log import Producer
3+
4+
5+
class LoadGroupScheduling(LoadScopeScheduling):
6+
"""Implement load scheduling across nodes, but grouping test only has group mark.
7+
8+
This distributes the tests collected across all nodes so each test is run
9+
just once. All nodes collect and submit the list of tests and when all
10+
collections are received it is verified they are identical collections.
11+
Then the collection gets divided up in work units, grouped by group mark
12+
(If there is no group mark, it is itself a group.), and those work units
13+
et submitted to nodes. Whenever a node finishes an item, it calls
14+
``.mark_test_complete()`` which will trigger the scheduler to assign more
15+
work units if the number of pending tests for the node falls below a low-watermark.
16+
17+
When created, ``numnodes`` defines how many nodes are expected to submit a
18+
collection. This is used to know when all nodes have finished collection.
19+
20+
This class behaves very much like LoadScopeScheduling,
21+
but with a itself or group(by marked) scope.
22+
"""
23+
24+
def __init__(self, config, log=None):
25+
super().__init__(config, log)
26+
if log is None:
27+
self.log = Producer("loadgroupsched")
28+
else:
29+
self.log = log.loadgroupsched
30+
31+
def _split_scope(self, nodeid):
32+
"""Determine the scope (grouping) of a nodeid.
33+
34+
There are usually 3 cases for a nodeid::
35+
36+
example/loadsuite/test/test_beta.py::test_beta0
37+
example/loadsuite/test/test_delta.py::Delta1::test_delta0
38+
example/loadsuite/epsilon/__init__.py::epsilon.epsilon
39+
40+
#. Function in a test module.
41+
#. Method of a class in a test module.
42+
#. Doctest in a function in a package.
43+
44+
With loadgroup, two cases are added::
45+
46+
example/loadsuite/test/test_beta.py::test_beta0
47+
example/loadsuite/test/test_delta.py::Delta1::test_delta0
48+
example/loadsuite/epsilon/__init__.py::epsilon.epsilon
49+
example/loadsuite/test/test_gamma.py::test_beta0@gname
50+
example/loadsuite/test/test_delta.py::Gamma1::test_gamma0@gname
51+
52+
This function will group tests with the scope determined by splitting
53+
the first ``@`` from the right. That is, test will be grouped in a
54+
single work unit when they have same group name.
55+
In the above example, scopes will be::
56+
57+
example/loadsuite/test/test_beta.py::test_beta0
58+
example/loadsuite/test/test_delta.py::Delta1::test_delta0
59+
example/loadsuite/epsilon/__init__.py::epsilon.epsilon
60+
gname
61+
gname
62+
"""
63+
if nodeid.rfind("@") > nodeid.rfind("]"):
64+
# check the index of ']' to avoid the case: parametrize mark value has '@'
65+
return nodeid.split("@")[-1]
66+
else:
67+
return nodeid

testing/acceptance_test.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,6 +1326,115 @@ def test_2():
13261326
assert c1 == c2
13271327

13281328

1329+
class TestGroupScope:
1330+
def test_by_module(self, testdir):
1331+
test_file = """
1332+
import pytest
1333+
class TestA:
1334+
@pytest.mark.xgroup(name="xgroup")
1335+
@pytest.mark.parametrize('i', range(5))
1336+
def test(self, i):
1337+
pass
1338+
"""
1339+
testdir.makepyfile(test_a=test_file, test_b=test_file)
1340+
result = testdir.runpytest("-n2", "--dist=loadgroup", "-v")
1341+
test_a_workers_and_test_count = get_workers_and_test_count_by_prefix(
1342+
"test_a.py::TestA", result.outlines
1343+
)
1344+
test_b_workers_and_test_count = get_workers_and_test_count_by_prefix(
1345+
"test_b.py::TestA", result.outlines
1346+
)
1347+
1348+
assert (
1349+
test_a_workers_and_test_count
1350+
in (
1351+
{"gw0": 5},
1352+
{"gw1": 0},
1353+
)
1354+
or test_a_workers_and_test_count in ({"gw0": 0}, {"gw1": 5})
1355+
)
1356+
assert (
1357+
test_b_workers_and_test_count
1358+
in (
1359+
{"gw0": 5},
1360+
{"gw1": 0},
1361+
)
1362+
or test_b_workers_and_test_count in ({"gw0": 0}, {"gw1": 5})
1363+
)
1364+
assert (
1365+
test_a_workers_and_test_count.items()
1366+
== test_b_workers_and_test_count.items()
1367+
)
1368+
1369+
def test_by_class(self, testdir):
1370+
testdir.makepyfile(
1371+
test_a="""
1372+
import pytest
1373+
class TestA:
1374+
@pytest.mark.xgroup(name="xgroup")
1375+
@pytest.mark.parametrize('i', range(10))
1376+
def test(self, i):
1377+
pass
1378+
class TestB:
1379+
@pytest.mark.xgroup(name="xgroup")
1380+
@pytest.mark.parametrize('i', range(10))
1381+
def test(self, i):
1382+
pass
1383+
"""
1384+
)
1385+
result = testdir.runpytest("-n2", "--dist=loadgroup", "-v")
1386+
test_a_workers_and_test_count = get_workers_and_test_count_by_prefix(
1387+
"test_a.py::TestA", result.outlines
1388+
)
1389+
test_b_workers_and_test_count = get_workers_and_test_count_by_prefix(
1390+
"test_a.py::TestB", result.outlines
1391+
)
1392+
1393+
assert (
1394+
test_a_workers_and_test_count
1395+
in (
1396+
{"gw0": 10},
1397+
{"gw1": 0},
1398+
)
1399+
or test_a_workers_and_test_count in ({"gw0": 0}, {"gw1": 10})
1400+
)
1401+
assert (
1402+
test_b_workers_and_test_count
1403+
in (
1404+
{"gw0": 10},
1405+
{"gw1": 0},
1406+
)
1407+
or test_b_workers_and_test_count in ({"gw0": 0}, {"gw1": 10})
1408+
)
1409+
assert (
1410+
test_a_workers_and_test_count.items()
1411+
== test_b_workers_and_test_count.items()
1412+
)
1413+
1414+
def test_module_single_start(self, testdir):
1415+
test_file1 = """
1416+
import pytest
1417+
@pytest.mark.xgroup(name="xgroup")
1418+
def test():
1419+
pass
1420+
"""
1421+
test_file2 = """
1422+
import pytest
1423+
def test_1():
1424+
pass
1425+
@pytest.mark.xgroup(name="xgroup")
1426+
def test_2():
1427+
pass
1428+
"""
1429+
testdir.makepyfile(test_a=test_file1, test_b=test_file1, test_c=test_file2)
1430+
result = testdir.runpytest("-n2", "--dist=loadgroup", "-v")
1431+
a = get_workers_and_test_count_by_prefix("test_a.py::test", result.outlines)
1432+
b = get_workers_and_test_count_by_prefix("test_b.py::test", result.outlines)
1433+
c = get_workers_and_test_count_by_prefix("test_c.py::test_2", result.outlines)
1434+
1435+
assert a.keys() == b.keys() and b.keys() == c.keys()
1436+
1437+
13291438
class TestLocking:
13301439
_test_content = """
13311440
class TestClassName%s(object):

0 commit comments

Comments
 (0)