Skip to content

Commit 226ba0a

Browse files
authored
xdist fixes (microsoft#23791)
- Don't use xdist when only running one test. this makes it slightly faster when running one test, because instead of creating a single worker it just runs the test in the same process - fix microsoft#23816 - fix issue where the plugin was being registered multiple times --------- Co-authored-by: detachhead <[email protected]>
1 parent 1cc490b commit 226ba0a

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

python_files/vscode_pytest/__init__.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
)
2020

2121
import pytest
22+
from pluggy import Result
2223

2324
script_dir = pathlib.Path(__file__).parent.parent
2425
sys.path.append(os.fspath(script_dir))
@@ -889,11 +890,25 @@ def send_post_request(
889890

890891
class DeferPlugin:
891892
@pytest.hookimpl(hookwrapper=True)
892-
def pytest_xdist_auto_num_workers(self, config: pytest.Config) -> Generator[None, int, int]:
893+
def pytest_xdist_auto_num_workers(
894+
self, config: pytest.Config
895+
) -> Generator[None, Result[int], None]:
893896
"""Determine how many workers to use based on how many tests were selected in the test explorer."""
894-
return min((yield), len(config.option.file_or_dir))
897+
outcome = yield
898+
result = min(outcome.get_result(), len(config.option.file_or_dir))
899+
if result == 1:
900+
result = 0
901+
outcome.force_result(result)
895902

896903

897904
def pytest_plugin_registered(plugin: object, manager: pytest.PytestPluginManager):
898-
if manager.hasplugin("xdist") and not isinstance(plugin, DeferPlugin):
899-
manager.register(DeferPlugin())
905+
plugin_name = "vscode_xdist"
906+
if (
907+
# only register the plugin if xdist is enabled:
908+
manager.hasplugin("xdist")
909+
# prevent infinite recursion:
910+
and not isinstance(plugin, DeferPlugin)
911+
# prevent this plugin from being registered multiple times:
912+
and not manager.hasplugin(plugin_name)
913+
):
914+
manager.register(DeferPlugin(), name=plugin_name)

0 commit comments

Comments
 (0)