You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enforce 90% pytest coverage in CI using pytest-cov 🧪 Tests⚙️ Configuration changes🕐 10-20 Minutes
Description
• Add pytest-cov to dev dependencies for local/CI coverage measurement.
• Run pytest with coverage reporting and fail CI below 90% threshold.
• Surface missing lines in CI output to guide test additions.
2. Add a dedicated coverage job/artifact upload (e.g., Codecov)
➕ Richer historical reporting and PR annotations
➕ Can keep CI pass/fail separate from reporting concerns
➖ Adds external dependency/service configuration
➖ More moving parts than an inline fail-under gate
Recommendation: The current approach (inline pytest-cov invocation with --cov-fail-under) is the simplest way to enforce a hard coverage gate in CI. If the team expects to tune coverage settings frequently or wants parity with local runs, consider moving the coverage flags into project config (pyproject.toml/pytest.ini) while keeping CI as the enforcement point.
Files changed (2) +7 / -3
Other (2) +7 / -3
cicd.ymlRun pytest with coverage and enforce 90% minimum in CI+3/-2
Run pytest with coverage and enforce 90% minimum in CI
• Updates the CI workflow to run pytest with pytest-cov enabled. Adds terminal missing-line reporting and fails the job if coverage is below 90%.
The action failed because the pytest coverage gate was not met: - pytest -q --cov=Algorithms --cov-report=term-missing --cov-fail-under=90 enforces a minimum total coverage of 90%. - The run completed with all tests passing (55 passed), but total coverage was only 80.24%, triggering: FAIL Required test coverage of 90% not reached. - Low-coverage modules include: - Algorithms/datastructs/hash_table/hash_table.py (49% coverage; many lines missing: 14, 20-23, 36-37, 40-46, 49-63, 66) - Algorithms/datastructs/hash_table/cache.py (86% coverage; missing 22-24, 33, 39, 51, 61-66) - Algorithms/datastructs/hash_table/bucket_hash_table.py (87% coverage; missing 14-18, 82-87) Pytest exited with code 1 due to the coverage failure, causing the GitHub Action step to fail.
Relevant error logs:
1: ##[group]Runner Image Provisioner2: Hosted Compute Agent
...
315: Successfully installed python-examples-0.1.0316: ##[group]Run pytest -q --cov=Algorithms --cov-report=term-missing --cov-fail-under=90317: �[36;1mpytest -q --cov=Algorithms --cov-report=term-missing --cov-fail-under=90�[0m318: shell: /usr/bin/bash -e {0}319: env:320: pythonLocation: /opt/hostedtoolcache/Python/3.12.13/x64321: PKG_CONFIG_PATH: /opt/hostedtoolcache/Python/3.12.13/x64/lib/pkgconfig322: Python_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.13/x64323: Python2_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.13/x64324: Python3_ROOT_DIR: /opt/hostedtoolcache/Python/3.12.13/x64325: LD_LIBRARY_PATH: /opt/hostedtoolcache/Python/3.12.13/x64/lib326: ##[endgroup]327: ....................................................... [100%]328: =============================== warnings summary ===============================329: ../../../../../opt/hostedtoolcache/Python/3.12.13/x64/lib/python3.12/site-packages/torch/_subclasses/functional_tensor.py:362330: /opt/hostedtoolcache/Python/3.12.13/x64/lib/python3.12/site-packages/torch/_subclasses/functional_tensor.py:362: UserWarning: Failed to initialize NumPy: No module named 'numpy' (Triggered internally at /pytorch/torch/csrc/utils/tensor_numpy.cpp:84.)331: cpu = _conversion_method_template(device=torch.device("cpu"))332: -- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html333: ---------- coverage: platform linux, python 3.12.13-final-0 ----------334: Name Stmts Miss Cover Missing335: --------------------------------------------------------------------------------------336: Algorithms/datastructs/hash_table/bucket_hash_table.py 77 10 87% 14-18, 82-87337: Algorithms/datastructs/hash_table/cache.py 87 12 86% 22-24, 33, 39, 51, 61-66338: Algorithms/datastructs/hash_table/hash_table.py 53 27 49% 14, 20-23, 36-37, 40-46, 49-63, 66339: Algorithms/slidingWindow/longestUnique.py 31 0 100%340: --------------------------------------------------------------------------------------341: TOTAL 248 49 80%342: FAIL Required test coverage of 90% not reached. Total coverage: 80.24%343: 55 passed, 1 warning in 2.69s344: ##[error]Process completed with exit code 1.345: Node 20 is being deprecated. This workflow is running with Node 24 by default. If you need to temporarily use Node 20, you can set the ACTIONS_ALLOW_USE_UNSECURE_NODE_VERSION=true environment variable. For more information see: https://github.blog/changelog/2025-09-19-deprecation-of-node-20-on-github-actions-runners/
CI now enforces --cov-fail-under=90 for Algorithms, but the current tests import modules that
contain significant method bodies that are never exercised (e.g., RandomizedCollection in
hash_table.py), which will reduce measured coverage and make the new gate prone to failing until
those lines are tested or excluded.
+ - name: Run tests with coverage+ run: |+ pytest -q --cov=Algorithms --cov-report=term-missing --cov-fail-under=90
Evidence
The workflow change introduces a hard 90% coverage gate. The test suite imports
Algorithms.datastructs.hash_table.hash_table, but only tests RandomizedSet; the same imported
module also defines RandomizedCollection and there are no test references to it anywhere, so its
method bodies remain uncovered and reduce coverage once enforced. Similarly, the imported cache.py
and bucket_hash_table.py define __str__/__repr__ helpers that are not called anywhere in
tests/, leaving more uncovered lines under the same --cov=Algorithms measurement scope.
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution
## Issue description
The workflow now runs pytest with coverage enforcement (`--cov-fail-under=90`). Several modules under `Algorithms/` that are imported by the existing tests contain code that is never executed by those tests (e.g., an entire extra class in `hash_table.py`, plus `__str__`/`__repr__` helpers), which reduces measured coverage and makes the 90% gate brittle.
## Issue Context
- `pytest-cov` measures executed vs. non-executed lines in imported source files under the specified `--cov` source.
- Current tests import these modules, but do not call the identified functions/methods, so their bodies remain uncovered.
## Fix Focus Areas
- .github/workflows/cicd.yml[29-31]
- Algorithms/datastructs/hash_table/hash_table.py[33-66]
- Algorithms/datastructs/hash_table/cache.py[60-66]
- Algorithms/datastructs/hash_table/bucket_hash_table.py[7-18]
- tests/algorithms/test_hash_table.py[1-40]
- tests/algorithms/test_cache.py[1-127]
- tests/algorithms/test_bucket_hash_table.py[1-120]
## What to change
Pick one (or combine):
1. **Add tests** that exercise `RandomizedCollection` and the `__str__`/`__repr__` helpers (simple smoke tests calling these methods is sufficient).
2. **Exclude intentionally-non-critical lines** from coverage (e.g., add `# pragma: no cover` to `__repr__`/`__str__`, or configure coverage omission via a config file).
3. **Adjust the gate scope/threshold** to match what is realistically covered today (e.g., lower `--cov-fail-under` or target a narrower `--cov` source until coverage is expanded).
ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Use pytest-cov to ensure test coverage of examples added.
Closes #42