- Add ENABLE_COVERAGE CMake option with gcov flags - Create collect-coverage.sh script for LCOV workflow - Add Qt6 test files with QTest framework - Update Jenkinsfile with Coverage stage (main/master/coverage.*) - Update CI Docker image with lcov and genhtml packages - Add .gitignore entries for coverage artifacts - Add COVERAGE.md documentation Coverage workflow: 1. Configure with -DENABLE_COVERAGE=ON 2. Build and run tests to generate .gcda/.gcno files 3. Collect coverage with lcov 4. Filter out Qt/system libraries 5. Generate HTML report and lcov.info for Jenkins Jenkins publishes coverage/lcov.info using Coverage Plugin. Coverage stage only runs on protected branches. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Entire-Checkpoint: 7783c7698334 Entire-Checkpoint: 70341e29996b
6.4 KiB
Code Coverage with LCOV
This document describes how to generate and view code coverage reports for the Qt6 migrated benchmark project.
Prerequisites
Local Development
Install the required tools:
# Ubuntu/Debian
sudo apt-get install lcov genhtml
# Fedora/RHEL
sudo dnf install lcov
# macOS (with Homebrew)
brew install lcov
CI/CD Environment
The CI Docker image (lifemaps/ci-cpp:latest) includes lcov and genhtml. If you're building a custom CI image, ensure these packages are installed:
RUN apt-get install -y --no-install-recommends \
lcov \
genhtml
Generating Coverage Reports
Local Coverage
-
Navigate to the project directory:
cd qt6-after/qt6-migrated-benchmark -
Run the coverage collection script:
./scripts/collect-coverage.shOr specify a custom build directory:
./scripts/collect-coverage.sh /path/to/build-dir -
View the HTML report:
xdg-open coverage/html/index.html # Linux open coverage/html/index.html # macOS
Manual Coverage Generation
If you prefer manual control:
# 1. Configure with coverage enabled
cmake -S qt6-after/qt6-migrated-benchmark -B build-coverage -G Ninja \
-DCMAKE_BUILD_TYPE=Debug \
-DENABLE_COVERAGE=ON
# 2. Build the project
cmake --build build-coverage
# 3. Run tests to generate coverage data
ctest --test-dir build-coverage --output-on-failure
# 4. Collect coverage data
lcov --directory build-coverage \
--capture \
--output-file coverage.info \
--ignore-errors mismatch \
--no-external
# 5. Filter out system and Qt libraries
lcov --remove coverage.info \
'/usr/*' \
'/opt/*' \
'*/Qt6/*' \
'*/tests/*' \
--output-file coverage.filtered.info \
--ignore-errors mismatch
# 6. Generate HTML report
genhtml coverage.filtered.info \
--output-directory coverage/html \
--branch-coverage \
--ignore-errors mismatch \
--title "Qt6 Migration Benchmark Coverage"
CI/CD Integration
Jenkins Pipeline
The Jenkinsfile automatically runs coverage collection on main, master, or branches matching coverage.*. The coverage report is published using the Jenkins Coverage Plugin.
Coverage Stage Configuration:
- Runs only on protected branches
- Expects
coverage/lcov.infoat workspace root - Publishes coverage trends across builds
Viewing Coverage in Jenkins:
- Navigate to your job in Jenkins
- Click on a build number
- Find the "Coverage Report" section in the sidebar
- Click to view detailed coverage statistics
Coverage Report Location
After the Jenkins build completes:
- LCOV data:
coverage/lcov.info(relative to workspace root) - HTML report: Available in Jenkins UI via Coverage Plugin
CMake Configuration
Coverage is controlled by the ENABLE_COVERAGE CMake option:
option(ENABLE_COVERAGE "Enable code coverage with gcov/lcov" OFF)
Coverage Flags
When enabled, the following flags are applied:
Compile flags:
-g- Debug symbols-O0- No optimization--coverage- Enable coverage instrumentation-fprofile-arcs- Generate arc profiling data-ftest-coverage- Generate coverage data
Link flags:
--coverage- Link with coverage libraries-fprofile-arcs- Link arc profiling support
Test Requirements
Coverage reports are only as good as your test suite. Ensure you have:
- Unit tests for individual functions and methods
- Integration tests for component interactions
- Edge case coverage for boundary conditions
- Error path coverage for exception handling
Running Tests
# Run all tests
ctest --test-dir build-coverage --output-on-failure
# Run specific test
ctest --test-dir build-coverage -R TestMainWindow --output-on-failure
# Run with verbose output
ctest --test-dir build-coverage -V
Interpreting Coverage Results
Coverage Metrics
- Lines: Percentage of executable lines executed
- Functions: Percentage of functions called
- Branches: Percentage of decision branches taken (if enabled)
Coverage Targets
Aim for:
- >80% line coverage for production code
- >70% branch coverage for critical paths
- 100% coverage for safety-critical modules
Coverage Report Structure
The HTML report includes:
- Summary table: Overall coverage statistics
- File list: Per-file coverage breakdown
- Source view: Line-by-line coverage with highlighting
- Green: Executed lines
- Red: Never executed lines
- Yellow: Partially executed branches
Troubleshooting
No Coverage Data Generated
Problem: lcov reports "no valid records found"
Solutions:
- Ensure tests actually run and complete successfully
- Verify
ENABLE_COVERAGE=ONwas set during CMake configuration - Check that
.gcdafiles are generated in the build directory - Ensure the same compiler is used for build and coverage collection
Mismatch Errors
Problem: lcov reports "version mismatch"
Solutions:
- Clean the build directory:
rm -rf build-coverage - Reconfigure and rebuild with coverage enabled
- Ensure consistent compiler versions
Low Coverage Numbers
Problem: Coverage is lower than expected
Solutions:
- Verify all tests are being executed (check CTest output)
- Check if code is conditionally compiled out
- Ensure test data exercises all code paths
- Review filtered paths in
collect-coverage.sh
Best Practices
- Run coverage regularly - Integrate into CI/CD pipeline
- Track trends - Monitor coverage over time, not just absolute values
- Focus on critical paths - Prioritize coverage of business logic
- Don't chase 100% - Aim for meaningful coverage, not vanity metrics
- Review uncovered code - Identify dead code or missing tests
Related Files
CMakeLists.txt- CMake configuration with coverage optionscripts/collect-coverage.sh- Automated coverage collection scriptJenkinsfile- CI/CD pipeline with coverage stageci/Dockerfile.ci- Docker image with coverage tools