Coverage for tests / select_plugin.py: 100.000%
3 statements
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-30 17:57 +0000
« prev ^ index » next coverage.py v7.12.1a0.dev1, created at 2025-11-30 17:57 +0000
1# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
2# For details: https://github.com/coveragepy/coveragepy/blob/main/NOTICE.txt
4"""
5A pytest plugin to select tests by running an external command.
7See lab/pick.py for how to use pick.py to subset test suites.
9More about this: https://nedbatchelder.com/blog/202401/randomly_subsetting_test_suites.html
11"""
13import subprocess 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%'()
16def pytest_addoption(parser): 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%'()
17 """Add command-line options for controlling the plugin."""
18 parser.addoption( 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%'()
19 "--select-cmd",
20 metavar="CMD",
21 action="store",
22 default="",
23 type=str,
24 help="Command to run to get test names",
25 )
28def pytest_collection_modifyitems(config, items): # pragma: debugging 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%'()
29 """Run an external command to get a list of tests to run."""
30 select_cmd = config.getoption("--select-cmd") 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%'()
31 if select_cmd: 1abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!#$%'()
32 output = subprocess.check_output(select_cmd, shell="True").decode("utf-8")
33 test_nodeids = {nodeid: seq for seq, nodeid in enumerate(output.splitlines())}
34 new_items = [item for item in items if item.nodeid in test_nodeids]
35 items[:] = sorted(new_items, key=lambda item: test_nodeids[item.nodeid])