Commit 0b3e060
Add test coverage workflow and dynamic percentage badge (#268)
Summary:
## Implementation Complete: Add Test Coverage Badge to README ✅
All tasks have been completed successfully:
- [x] Add `coverage` and `pytest-cov` to dev dependencies in `pyproject.toml`
- [x] Add `coverage` and `pytest-cov` to dev dependencies in `setup.py`
- [x] Create `.github/workflows/coverage.yml` workflow
- [x] Add dynamic coverage percentage badge to README.md
- [x] Configure coverage settings in `pyproject.toml`
- [x] Update `.gitignore` to exclude coverage artifacts
- [x] Apply PR review feedback:
- Fixed Protocol regex pattern to include opening parenthesis: `class .*\\b\\(Protocol\\):`
- Changed coverage extraction to use `coverage report --precision=2` instead of parsing XML
- Added `if: always()` to artifact upload steps to ensure coverage reports are uploaded even if tests fail
- [x] Fix coverage reporting issue:
- Use `python -m pytest` instead of `pytest` for better robustness
- Changed to `--cov-report=term-missing` to show missing coverage lines
- Display full coverage report table instead of trying to extract just percentage
- [x] Add CHANGELOG.md entry documenting the new coverage workflow and badge
- [x] Implement dynamic coverage percentage badge:
- Extract coverage percentage from test results
- Create shields.io endpoint badge JSON file (`.github/badges/coverage.json`)
- Commit badge data to repository on main branch
- Badge displays actual percentage (e.g., "75.5%") with color coding (green ≥80%, yellow ≥60%, orange <60%)
## Coverage Badge Implementation
The coverage badge now displays the actual test coverage percentage dynamically rather than just pass/fail status. The workflow:
1. Runs tests with coverage collection using `pytest-cov`
2. Extracts the total coverage percentage from the coverage report
3. Generates a shields.io-compatible JSON badge file with the percentage and color
4. Commits the badge JSON to `.github/badges/coverage.json` (only on main branch)
5. The README badge uses shields.io's endpoint feature to read from this JSON file
The badge color automatically adjusts based on coverage level:
- **Green (brightgreen)**: ≥80% coverage
- **Yellow**: ≥60% coverage
- **Orange**: <60% coverage
This solution uses shields.io for rendering but stores coverage data in the GitHub repository itself, maintaining the "no external service dependency" requirement for data storage.
<details>
<summary>Original prompt</summary>
>
> ----
>
> *This section details on the original issue you should resolve*
>
> <issue_title>[FEATURE] Add 'test coverage' badge to README.md</issue_title>
> <issue_description>implement coverage using github actions.
>
> 1. GitHub Actions Setup:
> Implement Python coverage collection using coverage.py
> Generate HTML reports and coverage badges locally
> 2. Coverage Implementation:
> # .github/workflows/coverage.yml
> - name: Generate Coverage Report
> run: |
> coverage run -m pytest
> coverage html
> coverage xml
> - name: Upload Coverage to Artifacts
> uses: actions/upload-artifact@v3
> with:
> name: coverage-report
> path: htmlcov/
> 3. Badge Generation:
> Use [shields.io](https://shields.io/) with coverage percentage from CI
> Store coverage data in repository or use GitHub's coverage API
> No external service dependency required</issue_description>
>
> <agent_instructions>If you can't do https://shields.io/ badge - write the PR of the github actions and I'll do that part manually</agent_instructions>
>
> ## Comments on the Issue (you are copilot in this section)
>
> <comments>
> <comment_new><author>talgalili</author><body>
> FYI:
> neuralsorcerer (this is simple, and would be nice given the work done recently on code coverage)
> </body></comment_new>
> <comment_new><author>talgalili</author><body>
> neuralsorcerer I moved this to me.</body></comment_new>
> </comments>
>
</details>
- Fixes #221
---
✨ Let Copilot coding agent [set things up for you](https://github.com/facebookresearch/balance/issues/new?title=✨+Set+up+Copilot+instructions&body=Configure%20instructions%20for%20this%20repository%20as%20documented%20in%20%5BBest%20practices%20for%20Copilot%20coding%20agent%20in%20your%20repository%5D%28https://gh.io/copilot-coding-agent-tips%29%2E%0A%0A%3COnboard%20this%20repo%3E&assignees=copilot) — coding agent works faster and does higher quality work when set up for your repo.
Pull Request resolved: #268
Differential Revision: D90883666
Pulled By: talgalili
fbshipit-source-id: 75ed93313f6ee188ca69b3663e9eb0c56e04f2581 parent c7fcb79 commit 0b3e060
File tree
7 files changed
+120
-0
lines changed- .github
- badges
- workflows
7 files changed
+120
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
7 | 16 | | |
8 | 17 | | |
9 | 18 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
80 | 81 | | |
81 | 82 | | |
82 | 83 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
57 | 57 | | |
58 | 58 | | |
59 | 59 | | |
| 60 | + | |
| 61 | + | |
60 | 62 | | |
61 | 63 | | |
62 | 64 | | |
| |||
101 | 103 | | |
102 | 104 | | |
103 | 105 | | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
| 38 | + | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| |||
0 commit comments