From b17282e51890b09bc83a075c601238a234771ac4 Mon Sep 17 00:00:00 2001 From: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com> Date: Thu, 18 Dec 2025 10:46:00 -0800 Subject: [PATCH] fix(tests): enhance pytest-cov detection --- python_files/tests/pytestadapter/helpers.py | 39 +++++++++++++------ .../tests/pytestadapter/test_coverage.py | 20 ++++++++++ python_files/vscode_pytest/__init__.py | 4 +- 3 files changed, 51 insertions(+), 12 deletions(-) diff --git a/python_files/tests/pytestadapter/helpers.py b/python_files/tests/pytestadapter/helpers.py index 25e6187e2efa..03f1187149df 100644 --- a/python_files/tests/pytestadapter/helpers.py +++ b/python_files/tests/pytestadapter/helpers.py @@ -264,17 +264,34 @@ def runner_with_cwd_env( pipe_name = generate_random_pipe_name("pytest-discovery-test") if "COVERAGE_ENABLED" in env_add and "_TEST_VAR_UNITTEST" not in env_add: - process_args = [ - sys.executable, - "-m", - "pytest", - "-p", - "vscode_pytest", - "--cov=.", - "--cov-branch", - "-s", - *args, - ] + if "_PYTEST_MANUAL_PLUGIN_LOAD" in env_add: + # Test manual plugin loading scenario for issue #25590 + process_args = [ + sys.executable, + "-m", + "pytest", + "--disable-plugin-autoload", + "-p", + "pytest_cov.plugin", + "-p", + "vscode_pytest", + "--cov=.", + "--cov-branch", + "-s", + *args, + ] + else: + process_args = [ + sys.executable, + "-m", + "pytest", + "-p", + "vscode_pytest", + "--cov=.", + "--cov-branch", + "-s", + *args, + ] # Generate pipe name, pipe name specific per OS type. diff --git a/python_files/tests/pytestadapter/test_coverage.py b/python_files/tests/pytestadapter/test_coverage.py index d2d276172a8d..f2387527698f 100644 --- a/python_files/tests/pytestadapter/test_coverage.py +++ b/python_files/tests/pytestadapter/test_coverage.py @@ -142,3 +142,23 @@ def test_coverage_w_omit_config(): assert results # assert one file is reported and one file (as specified in pyproject.toml) is omitted assert len(results) == 1 + + +def test_pytest_cov_manual_plugin_loading(): + """ + Test that pytest-cov is detected when loaded manually via -p pytest_cov.plugin. + + This test verifies the fix for issue #25590, where pytest-cov detection failed + when using --disable-plugin-autoload with -p pytest_cov.plugin. The plugin is + registered under its module name (pytest_cov.plugin) instead of entry point name + (pytest_cov) in this scenario. + """ + args = ["--collect-only"] + env_add = {"COVERAGE_ENABLED": "True", "_PYTEST_MANUAL_PLUGIN_LOAD": "True"} + cov_folder_path = TEST_DATA_PATH / "coverage_gen" + + # Should NOT raise VSCodePytestError about pytest-cov not being installed + actual = runner_with_cwd_env(args, cov_folder_path, env_add) + assert actual is not None + # Verify discovery succeeded (status != "error") + assert actual[0].get("status") != "error" diff --git a/python_files/vscode_pytest/__init__.py b/python_files/vscode_pytest/__init__.py index 1222a324b232..89565dab1264 100644 --- a/python_files/vscode_pytest/__init__.py +++ b/python_files/vscode_pytest/__init__.py @@ -87,7 +87,9 @@ def __init__(self, message): def pytest_load_initial_conftests(early_config, parser, args): # noqa: ARG001 - has_pytest_cov = early_config.pluginmanager.hasplugin("pytest_cov") + has_pytest_cov = early_config.pluginmanager.hasplugin( + "pytest_cov" + ) or early_config.pluginmanager.hasplugin("pytest_cov.plugin") has_cov_arg = any("--cov" in arg for arg in args) if has_cov_arg and not has_pytest_cov: raise VSCodePytestError(