test:Improving test coverage
Test Coverage Improvements
Overview
This document describes test coverage improvements for two key modules in the GitLab Compliance Checker project.
Files Modified
1. modes/bad_issue.py (Test Coverage: 64% → 100%)
Test File: tests/test_bad_issue.py
Changes Made:
- Refactored deeply nested mocks into reusable pytest fixtures
- Added comprehensive single user fetch tests covering previously uncovered UI sections (lines 109-154)
- Added summary metrics tests for total closed issues and total users
- Improved test readability with clear test naming and documentation
New Test Classes:
-
TestDummyComponents- Tests for mock context managers -
TestCachedFunctions- Tests for cached data functions -
TestSingleUserFetch- Tests for single user fetch functionality (NEW) -
TestSummaryMetrics- Tests for metrics rendering (NEW)
Key Test Coverage Additions:
| Function/Section | Before | After |
|---|---|---|
cached_batch_evaluate_issues |
|
|
cached_single_user_issues |
|
|
| Batch report generation |
|
|
| Single user fetch |
|
|
| Excel export error handling |
|
|
| Summary metrics rendering |
|
|
2. user_profile/render_user_profile.py (Test Coverage: 96% → 100%)
Test File: tests/test_render_user_profile.py
Changes Made:
- Added GitlabGetError exception handling test covering lines 97-98
- Improved test coverage for error scenarios in user profile rendering
New Test:
def test_render_user_profile_gitlab_get_error():
"""Test GitlabGetError exception handling (covers lines 97-98)."""
Key Test Coverage Additions:
| Function/Section | Before | After |
|---|---|---|
| User profile fetch |
|
|
| Account statistics |
|
|
| Issues summary |
|
|
| Detailed issues table |
|
|
| Profile README status |
|
|
| GitlabGetError exception |
|
|
How to Validate Locally
1. Run Specific Test Files
cd /home/suma/gitlab-compliance-checker
source venv/bin/activate
# Test bad_issue.py coverage
pytest tests/test_bad_issue.py -v
# Test render_user_profile.py coverage
pytest tests/test_render_user_profile.py -v
2. Verify Coverage
# Check coverage for bad_issue.py
pytest --cov=modes/bad_issue --cov-report=term-missing tests/test_bad_issue.py
# Check coverage for render_user_profile.py
pytest --cov=user_profile/render_user_profile --cov-report=term-missing tests/test_render_user_profile.py
3. Expected Results
modes/bad_issue.py 75 0 100%
user_profile/render_user_profile.py 52 0 100%
tests/test_bad_issue.py 233 0 100%
tests/test_render_user_profile.py 127 0 100%
4. Run Full Test Suite
pytest tests/ -v
Expected: All tests pass (477+ tests)
Test Design Improvements
Before (Deeply Nested Mocks):
# ❌ Poor pattern - 5+ levels of nesting
with patch("modes.bad_issue.st"):
with patch("modes.bad_issue.cached_batch_evaluate_issues"):
with patch("modes.bad_issue.BATCH_USERNAMES"):
with patch("modes.bad_issue.st.columns"):
with patch("modes.bad_issue.st.button"):
# Test code here
After (Reusable Fixtures):
# ✅ Improved pattern - Clean fixtures
@pytest.fixture
def mock_streamlit():
with patch("modes.bad_issue.st") as mock_st:
mock_st.button = MagicMock(return_value=False)
mock_st.columns = MagicMock(side_effect=make_columns)
# ... setup
yield mock_st
def test_single_user_success(mock_streamlit, mock_client):
mock_streamlit.button.return_value = True
render_bad_issue_batch_ui(mock_client)
Coverage Improvement Summary
| Metric | Before | After | Improvement |
|---|---|---|---|
modes/bad_issue.py |
64% | 100% | +36% |
user_profile/render_user_profile.py |
96% | 100% | +4% |
| Overall Project | 96% | 97% | +1% |
MR Acceptance Checklist
Evaluate this MR against the MR acceptance checklist.
-
Tests: All new tests pass -
Coverage: Test coverage improved significantly -
Code Quality: Ruff checks pass, code formatted -
Documentation: Test improvements documented -
No Breaking Changes: Existing tests unchanged -
Maintainability: Refactored nested mocks to fixtures
Related Files
-
tests/test_bad_issue.py- Refactored test file -
tests/test_render_user_profile.py- Enhanced test file -
modes/bad_issue.py- Source file (unchanged) -
user_profile/render_user_profile.py- Source file (unchanged)
Edited by Suma Pullaiahgari