Skip to content

TPI: test python branch coverageΒ #25014

@eleanorjboyd

Description

@eleanorjboyd

Refs: #24980

Complexity: 4

Author: @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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions