Skip to content

Commit d079322

Browse files
Add hook to vscode_pytest to determine number xdist workers to use based on count of selected tests (#23539)
fixes microsoft/vscode-python-debugger#336 --------- Co-authored-by: detachhead <[email protected]> Co-authored-by: Karthik Nadig <[email protected]>
1 parent c4c48fd commit d079322

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

python_files/vscode_pytest/__init__.py

+22-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@
1414
script_dir = pathlib.Path(__file__).parent.parent
1515
sys.path.append(os.fspath(script_dir))
1616
sys.path.append(os.fspath(script_dir / "lib" / "python"))
17-
1817
from testing_tools import socket_manager # noqa: E402
19-
from typing import Any, Dict, List, Optional, Union, TypedDict, Literal # noqa: E402
18+
from typing import ( # noqa: E402
19+
Any,
20+
Dict,
21+
List,
22+
Optional,
23+
Union,
24+
TypedDict,
25+
Literal,
26+
Generator,
27+
)
2028

2129

2230
class TestData(TypedDict):
@@ -882,3 +890,15 @@ def send_post_request(
882890
f"Plugin error, exception thrown while attempting to send data[vscode-pytest]: {error} \n[vscode-pytest] data: \n{data}\n",
883891
file=sys.stderr,
884892
)
893+
894+
895+
class DeferPlugin:
896+
@pytest.hookimpl(wrapper=True)
897+
def pytest_xdist_auto_num_workers(self, config: pytest.Config) -> Generator[None, int, int]:
898+
"""determine how many workers to use based on how many tests were selected in the test explorer"""
899+
return min((yield), len(config.option.file_or_dir))
900+
901+
902+
def pytest_plugin_registered(plugin: object, manager: pytest.PytestPluginManager):
903+
if manager.hasplugin("xdist") and not isinstance(plugin, DeferPlugin):
904+
manager.register(DeferPlugin())

0 commit comments

Comments
 (0)