Skip to content

Commit 94a165f

Browse files
feat: Configure SonarCloud integration for Python coverage
- Added XML coverage report generation to pytest.ini - Updated Makefile coverage target to generate coverage.xml - Modified unit.sh to run 'coverage' instead of 'test' for Python projects - Configured sonar-scanner.properties with Python test paths and coverage: - Added src/asyncapigenerator/tests to sonar.tests - Added src/**/tests/ pattern to test inclusions - Enabled sonar.python.coverage.reportPaths - Added Python tests to coverage exclusions - Documented SonarCloud requirements in TESTING_PLAN.md - Added clean-test target to remove coverage.xml This fixes SonarCloud showing 0% coverage for Python projects.
1 parent bb8d24b commit 94a165f

File tree

5 files changed

+71
-10
lines changed

5 files changed

+71
-10
lines changed

scripts/config/sonar-scanner.properties

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ sonar.host.url=https://sonarcloud.io
44
sonar.qualitygate.wait=true
55
sonar.sourceEncoding=UTF-8
66
sonar.sources=.
7-
sonar.tests=tests/, lambdas/mesh-poll/src/__tests__, lambdas/ttl-create-lambda/src/__tests__, lambdas/ttl-poll-lambda/src/__tests__, utils/utils/src/__tests__
8-
sonar.test.inclusions=tests/**, lambdas/**/src/__tests__/**, utils/utils/src/__tests__/**
7+
sonar.tests=tests/, src/asyncapigenerator/tests, lambdas/mesh-poll/src/__tests__, lambdas/ttl-create-lambda/src/__tests__, lambdas/ttl-poll-lambda/src/__tests__, utils/utils/src/__tests__
8+
sonar.test.inclusions=tests/**, src/**/tests/**, lambdas/**/src/__tests__/**, utils/utils/src/__tests__/**
99
sonar.terraform.provider.aws.version=5.54.1
1010
sonar.cpd.exclusions=**.test.*
11-
sonar.coverage.exclusions=tests/, **/*.dev.*, lambdas/**/src/__tests__, **/jest.config.ts, scripts/**/*, docs/**/*, utils/utils/src/__tests__
11+
sonar.coverage.exclusions=tests/, src/**/tests/, **/*.dev.*, lambdas/**/src/__tests__, **/jest.config.ts, scripts/**/*, docs/**/*, utils/utils/src/__tests__
1212

13-
#sonar.python.coverage.reportPaths=.coverage/coverage.xml
13+
sonar.python.coverage.reportPaths=src/asyncapigenerator/coverage.xml
1414
sonar.javascript.lcov.reportPaths=lcov.info

scripts/tests/unit.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ cd "$(git rev-parse --show-toplevel)"
2222
# Python projects - asyncapigenerator
2323
echo "Setting up and running asyncapigenerator tests..."
2424
make -C ./src/asyncapigenerator install-dev
25-
make -C ./src/asyncapigenerator test
25+
make -C ./src/asyncapigenerator coverage # Run with coverage to generate coverage.xml for SonarCloud
2626

2727
# TypeScript/JavaScript projects (npm workspace)
2828
npm ci

src/TESTING_PLAN.md

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,28 @@ This document outlines the comprehensive plan for implementing unit tests across
7070

7171
**Track all implementation activities here. Add new entries at the top (reverse chronological order).**
7272

73+
### 2025-11-04 13:12 GMT - Added SonarCloud Integration for Python Coverage
74+
75+
- **Author**: GitHub Copilot
76+
- **Activity**: Configured Python test coverage reporting for SonarCloud
77+
- **Changes**:
78+
- Updated `src/asyncapigenerator/pytest.ini`: Added `--cov-report=xml:coverage.xml` to generate XML coverage reports
79+
- Updated `src/asyncapigenerator/Makefile`: Modified `coverage` target to include `--cov-report=xml:coverage.xml`
80+
- Updated `src/asyncapigenerator/Makefile`: Added `coverage.xml` to `clean-test` target
81+
- Updated `scripts/tests/unit.sh`: Changed from `make test` to `make coverage` for asyncapigenerator to generate coverage.xml
82+
- Updated `scripts/config/sonar-scanner.properties`:
83+
- Added `src/asyncapigenerator/tests` to `sonar.tests`
84+
- Added `src/**/tests/` pattern to `sonar.test.inclusions`
85+
- Added `src/**/tests/` to `sonar.coverage.exclusions`
86+
- Enabled `sonar.python.coverage.reportPaths=src/asyncapigenerator/coverage.xml` (was commented out)
87+
- **Files Modified**:
88+
- `src/asyncapigenerator/pytest.ini` - Added XML coverage report
89+
- `src/asyncapigenerator/Makefile` - Updated coverage target and clean-test
90+
- `scripts/tests/unit.sh` - Run coverage instead of test
91+
- `scripts/config/sonar-scanner.properties` - Added Python test paths and coverage configuration
92+
- **Rationale**: SonarCloud was showing 0% coverage because pytest wasn't generating XML coverage reports and sonar-scanner wasn't configured to find Python tests/coverage
93+
- **Status**: Python coverage should now be reported to SonarCloud on next CI run
94+
7395
### 2025-11-04 13:02 GMT - Fixed Timestamps and Updated Instructions
7496

7597
- **Author**: GitHub Copilot
@@ -777,11 +799,11 @@ cd "$(git rev-parse --show-toplevel)"
777799
# Python projects
778800
echo "Setting up and running asyncapigenerator tests..."
779801
make -C ./src/asyncapigenerator install-dev
780-
make -C ./src/asyncapigenerator test
802+
make -C ./src/asyncapigenerator coverage # Use coverage to generate coverage.xml for SonarCloud
781803

782804
echo "Setting up and running cloudeventjekylldocs tests..."
783805
make -C ./src/cloudeventjekylldocs install-dev
784-
make -C ./src/cloudeventjekylldocs test
806+
make -C ./src/cloudeventjekylldocs coverage # Use coverage to generate coverage.xml for SonarCloud
785807

786808
# TypeScript/JavaScript projects (npm workspace)
787809
npm ci
@@ -811,9 +833,46 @@ The GitHub Actions workflow (`.github/workflows/stage-2-test.yaml`):
811833

812834
- Python projects must have `install-dev` target in their Makefile
813835
- Python projects must have `requirements-dev.txt` with test dependencies
836+
- **Python projects must have `coverage` target that generates `coverage.xml`** for SonarCloud
814837
- Tests must run quickly (timeout is 5 minutes for unit tests)
815-
- Coverage reports should be in `.reports/` directory
816-
- Python coverage should use pytest-cov format compatible with lcov-result-merger
838+
- Coverage reports should be in `.reports/` directory for TypeScript, in project root for Python
839+
- Python coverage must generate XML format: `pytest --cov=. --cov-report=xml:coverage.xml`
840+
841+
### SonarCloud Integration
842+
843+
**For Python Projects**: SonarCloud requires XML coverage reports. Ensure:
844+
845+
1. **pytest.ini includes XML coverage**:
846+
847+
```ini
848+
addopts =
849+
--cov=.
850+
--cov-report=xml:coverage.xml
851+
```
852+
853+
2. **Makefile coverage target generates XML**:
854+
855+
```makefile
856+
coverage: ## Run tests with coverage report
857+
pytest tests/ --cov=. --cov-report=html --cov-report=term-missing --cov-report=xml:coverage.xml
858+
```
859+
860+
3. **scripts/tests/unit.sh runs coverage (not just test)**:
861+
862+
```bash
863+
make -C ./src/your-project coverage # Generates coverage.xml
864+
```
865+
866+
4. **scripts/config/sonar-scanner.properties configured**:
867+
868+
```properties
869+
sonar.tests=tests/, src/your-project/tests, ...
870+
sonar.test.inclusions=tests/**, src/**/tests/**, ...
871+
sonar.coverage.exclusions=tests/, src/**/tests/, ...
872+
sonar.python.coverage.reportPaths=src/your-project/coverage.xml
873+
```
874+
875+
Without these configurations, SonarCloud will show 0% coverage for Python projects.
817876

818877
### Testing CI/CD Changes Locally
819878

src/asyncapigenerator/Makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ test-verbose: ## Run tests with verbose output
3232
pytest tests/ -vv -s
3333

3434
coverage: ## Run tests with coverage report
35-
pytest tests/ --cov=. --cov-report=html --cov-report=term-missing
35+
pytest tests/ --cov=. --cov-report=html --cov-report=term-missing --cov-report=xml:coverage.xml
3636

3737
lint: ## Run linting
3838
flake8 .
@@ -45,6 +45,7 @@ clean-test: ## Clean test artifacts
4545
rm -rf .pytest_cache
4646
rm -rf htmlcov
4747
rm -rf .coverage
48+
rm -rf coverage.xml
4849
find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
4950

5051
validate: ## Validate generated AsyncAPI specs (requires asyncapi CLI)

src/asyncapigenerator/pytest.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ addopts =
1010
--cov=.
1111
--cov-report=html
1212
--cov-report=term-missing
13+
--cov-report=xml:coverage.xml
1314
--cov-config=pytest.ini
1415
markers =
1516
unit: Unit tests

0 commit comments

Comments
 (0)