Skip to content

Fix pytest coverage without pytest-cov - #26151

Open
Shayaan Tanveer (babaJaan01) wants to merge 2 commits into
microsoft:mainfrom
babaJaan01:fix/25724-coverage-without-pytest-cov
Open

Shayaan Tanveer (babaJaan01) wants to merge 2 commits into
microsoft:mainfrom
babaJaan01:fix/25724-coverage-without-pytest-cov

Conversation

@babaJaan01

Copy link
Copy Markdown

Fixes #25724

Fall back to coverage.py when coverage is requested without pytest-cov installed. Branch coverage is now derived from the collected coverage data.

Testing:

  • .venv/bin/python -m pytest python_files/tests/pytestadapter/test_coverage.py -q
  • .venv/bin/python python_files/tests/run_all.py — 224 passed, 2 skipped
  • Ruff lint and formatting checks
  • Extension compilation
  • Manual Extension Development Host test using pytest and coverage without pytest-cov; coverage displayed successfully in the Testing view.

@bschnurr

Bill Schnurr (bschnurr) commented Sep 15, 2026

Copy link
Copy Markdown
Member

🔒 Automated review in progress — Bill Schnurr (@bschnurr) is auto-reviewing this PR.

@babaJaan01

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

if importlib.util.find_spec("pytest_cov") is not None:
return [*args, "--cov=.", "--cov-branch"], None

import coverage

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Issue · Please address or respond

coverage.Coverage() defaults to branch=False, so this fallback never records arcs. As a result, has_branch_coverage() will always disable branch coverage when pytest-cov is absent, unlike the existing --cov-branch path. Initialize the fallback with branch=True (and equivalent source configuration if needed) and assert the collected fallback data contains branch arcs.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so updated the fallback to use coverage.Coverage(branch=True) and also added a test to confirm the no-pytest-cov fallback saves branch arcs

@bschnurr

Copy link
Copy Markdown
Member

Result: ⚠️ partially-verified

Verification details

Verification: Isolated verification observed failures that were not classified as caused by this PR: Dependency and test discovery preflight, Offline Python dependency bootstrap. The relevant tests could not be fully run in the isolated environment; this review is not fully verified.

Summary: The fallback coverage lifecycle was exercised with a fake coverage module and passed, and all changed Python files compiled successfully. The PR's targeted pytest suite could not run because the offline wheelhouse lacked `coverage`, causing dependency bootstrap to fail and leaving the virtual environment without pytest. Consequently, actual coverage collection, reporting, and branch detection remain unverified. Confidence is limited to the fallback plugin lifecycle and syntax.

Test runs: 2 passed, 2 failed, 1 not run

  • ⚠️ Not run | pytest adapter coverage tests | .venv/bin/python -m pytest python_files/tests/pytestadapter/test_coverage.py -q
  • Failed | unrelated to this PR | Dependency and test discovery preflight | printf 'AUTOMATION_SANDBOX_PROFILE=%s\n' "${AUTOMATION_SANDBOX_PROFILE:-}"; printf '%s\n' '--- test requirements ---'; grep -E '^(pytest|pytest-cov|coverage|packaging)' build/test-requirements.txt || true; printf '%s\n' '--- local dependency state ---'; test -d .venv && echo '.venv present' || echo '.venv absent'; test -d node_modules && echo 'node_modules present' || echo 'node_modules absent'; command -v uv || true; python - <<'PY'
    import importlib.util, sys
    print('python', sys.version.replace('\n', ' '))
    for name in ('pytest', 'coverage', 'packaging', 'pytest_cov'):
    spec = importlib.util.find_spec(name)
    print(name, 'present' if spec else 'missing', getattr(import(name), 'version', '') if spec else '')
    PY
    printf '%s\n' '--- changed files ---'; git diff --name-status HEAD^ HEAD
  • Failed | unrelated to this PR | Offline Python dependency bootstrap | python -m venv --system-site-packages .venv && .venv/bin/python -m pip install --no-index --find-links="${PIP_FIND_LINKS:-/opt/python-wheelhouse}" coverage pytest-cov pytest-json pytest-timeout pytest-describe pytest-ruff
  • Passed | Changed Python files compile | python -m py_compile python_files/vscode_pytest/init.py python_files/vscode_pytest/run_pytest_script.py python_files/tests/pytestadapter/test_coverage.py
  • Passed | Fallback coverage lifecycle | python - <<'PY'
    import sys, types
    from python_files.vscode_pytest import run_pytest_script
    class FakeCoverage:
    def init(self): self.events = []
    def start(self): self.events.append('start')
    def stop(self): self.events.append('stop')
    def save(self): self.events.append('save')
    fake = FakeCoverage()
    sys.modules['coverage'] = types.SimpleNamespace(Coverage=lambda: fake)
    original = run_pytest_script.importlib.util.find_spec
    run_pytest_script.importlib.util.find_spec = lambda name: None
    run_pytest_script.os.environ['COVERAGE_ENABLED'] = 'True'
    try:
    args, plugin = run_pytest_script.configure_coverage([])
    assert args == [] and plugin is not None and fake.events == ['start']
    plugin.pytest_sessionfinish(None, 0)
    assert fake.events == ['start', 'stop', 'save']
    finally:
    run_pytest_script.importlib.util.find_spec = original
    print('fallback coverage lifecycle passed:', fake.events)
    PY
⚠️ pytest adapter coverage tests diagnostic output
/workspace/.venv/bin/python: No module named pytest
Dependency and test discovery preflight diagnostic output
AUTOMATION_SANDBOX_PROFILE=typescript
.venv absent
node_modules absent
python 3.13.15
pytest present 9.1.1
coverage missing
packaging present 26.3
pytest_cov missing
error: Could not access 'HEAD^'
Offline Python dependency bootstrap diagnostic output
Looking in links: /opt/python-wheelhouse, /opt/python-wheelhouse
ERROR: Could not find a version that satisfies the requirement coverage (from versions: none)
ERROR: No matching distribution found for coverage

@bschnurr Bill Schnurr (bschnurr) added the review-auto:changes-requested Automated review: posted blocking findings to address. label Sep 15, 2026
@bschnurr

Copy link
Copy Markdown
Member

Result: ⚠️ partially-verified

Verification details

Verification: Isolated verification observed failures that were not classified as caused by this PR: pytest adapter coverage tests, Offline Python dependency bootstrap, Fallback smoke check from package directory, Fallback smoke check using created virtual environment.

Summary: The targeted coverage test module could not collect because `coverage` was unavailable, and the offline wheelhouse could not install it. A dependency-free ad-hoc check passed for fallback lifecycle, pytest-cov selection, user arguments, plugin registration, and branch-arc detection. No PR-caused failure was observed, but real coverage collection/reporting remains unverified in this environment.

Test runs: 2 passed, 4 failed

  • Failed | unrelated to this PR | pytest adapter coverage tests | python -m pytest python_files/tests/pytestadapter/test_coverage.py -q
  • Failed | unrelated to this PR | Offline Python dependency bootstrap | python -m venv --system-site-packages .venv && .venv/bin/python -m pip install --no-index --find-links="$PIP_FIND_LINKS" coverage pytest-cov
  • Failed | unrelated to this PR | Fallback smoke check from package directory | .venv/bin/python - <<'PY'
    ...
    PY
  • Failed | unrelated to this PR | Fallback smoke check using created virtual environment | PYTHONPATH=python_files .venv/bin/python - <<'PY'
    ...
    PY
  • Passed | Dependency and test discovery | printf 'PROFILE=%s\n' "$AUTOMATION_SANDBOX_PROFILE"; git diff --name-status HEAD^ HEAD; printf '\nPython/test tooling:\n'; python --version; python - <<'PY'
    mods = ['pytest', 'coverage', 'pytest_cov']
    import importlib.util
    for m in mods:
    print(m, bool(importlib.util.find_spec(m)))
    PY
    printf '\nRelevant manifests:\n'; ls -1 pyproject.toml python_files/requirements*.txt python_files/tests/requirements*.txt 2>/dev/null || true; git diff --unified=0 HEAD^ HEAD -- python_files/tests/pytestadapter/test_coverage.py | grep '^+def test_' || true
  • Passed | Coverage fallback and branch-detection smoke test | PYTHONPATH=python_files python - <<'PY'

Injects a minimal Coverage test double and exercises configure_coverage,

run_pytest plugin registration, plugin shutdown, and has_branch_coverage.

PY

pytest adapter coverage tests diagnostic output
ImportError while importing test module '/workspace/python_files/tests/pytestadapter/test_coverage.py'.
python_files/tests/pytestadapter/test_coverage.py:9: in <module>
    import coverage
E   ModuleNotFoundError: No module named 'coverage'
1 error in 0.17s
Offline Python dependency bootstrap diagnostic output
Looking in links: /opt/python-wheelhouse, /opt/python-wheelhouse
ERROR: Could not find a version that satisfies the requirement coverage
ERROR: No matching distribution found for coverage
Fallback smoke check from package directory diagnostic output
/bin/sh: 1: .venv/bin/python: not found
Fallback smoke check using created virtual environment diagnostic output
ModuleNotFoundError: No module named 'pytest'

@bschnurr Bill Schnurr (bschnurr) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

@bschnurr Bill Schnurr (bschnurr) added review-auto:approved Automated review: no blocking findings (approval posted). and removed review-auto:changes-requested Automated review: posted blocking findings to address. labels Sep 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review-auto:approved Automated review: no blocking findings (approval posted).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow collecting coverage without pytest-cov

2 participants