Skip to content

TPI: test python branch coverage #25014

Description

Refs: #24980

Complexity: 4

Author: Eleanor Boyd (@eleanorjboyd)

Create Issue

Create Issue


✅ Test Plan: Verify Coverage Integration for Pytest & Unittest

📌 Objective

Ensure test branch coverage is correctly reported in VS Code using both pytest and unittest, and that the results align with the command-line coverage output (with minor rounding differences expected).

🧪 Repo & Project Info

Project Structure:

coverage-example-repo/
├── src/
│   └── math_utils.py
├── test/
│   ├── test_math_utils_pytest.py
│   └── test_math_utils_unittest.py
├── requirements.txt
└── README.md

🛠 Setup

Clone this repo: coverage-example-repo

git clone https://github.com/eleanorjboyd/coverage-example-repo
cd coverage-example-repo

In VS Code, open the Command Palette → Python: Create Environment

If the UI does not prompt to install requirements, run:

pip install -r requirements.txt

✅ Part 1: Test with Pytest

  • Open the Testing tab in VS Code
  • Select pytest as the test framework
  • Click the 3rd run button (▶️ with checkmark) to run tests with coverage
  • Coverage results are visible
  • Branch coverage is shown
  • Coverage numbers look reasonable

✅ Part 2: Test with Unittest

  • Change the test framework to unittest (settings.json or VS Code UI)
  • Update any necessary args in python.testing.unittestArgs
  • Click the same 3rd run button to run tests with coverage
  • Confirm:
    • Coverage results are visible
    • Branch coverage is shown
    • Coverage numbers make sense and are comparable to pytest

✅ Part 3: Cross-check with CLI Coverage

  • Run tests with pytest from the CLI:
coverage run --branch -m pytest
coverage report

Erase and re-run with unittest:

coverage erase
coverage run --branch -m unittest discover -s test
coverage report

Confirm:

  • CLI coverage output branch coverage is the same as the output from the UI
    ⚠️ Note: minor rounding differences are expected

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions