Overview
BuckSample includes built-in support for generating code coverage reports using LLVM’s coverage tools. This allows you to measure how much of your code is being tested and identify untested code paths.Configuration
Code coverage is configured using a dedicated Buck configuration file that adds the necessary compiler flags.Coverage Flags
Thecode_coverage.buckconfig file defines the flags needed for instrumentation:
code_coverage.buckconfig
.buckconfig file:
.buckconfig
The
$(config ...) syntax dynamically references values from the coverage configuration file, allowing you to enable coverage instrumentation without modifying your main build configuration.Generating Coverage Reports
Using the Test Command
Themake test command runs tests with coverage enabled:
Run Tests with Coverage
Execute the test command from your project root:This command performs several operations:
- Cleans previous coverage data
- Runs tests with LLVM profiling enabled
- Merges raw profile data
- Generates a coverage report
How It Works
The Makefile implements the coverage workflow:Makefile
Run Instrumented Tests
Runs Buck tests with the
XCTOOL_TEST_ENV_LLVM_PROFILE_FILE environment variable set to specify where raw coverage data should be written. The --config-file flag applies the coverage instrumentation flags.Merge Profile Data
Uses The
llvm-profdata merge to combine all .profraw files into a single .profdata file:-sparse flag creates a more compact profile data file.LLVM Coverage Tools
llvm-profdata
Merges raw profile data from multiple test runs into a single indexed profile:-sparse: Create a compact profile data file-o: Specify output file path
llvm-cov
Generates coverage reports from indexed profile data:Output Files
Coverage data is stored in Buck’s output directory:- Raw profiles:
buck-out/tmp/code-*.profraw- Individual profile data from test processes - Merged profile:
buck-out/gen/Coverage.profdata- Combined and indexed coverage data - Test bundle: Located via
buck targets //App:ExampleAppCITests --show-output
Best Practices
Troubleshooting
No Coverage Data Generated
If no.profraw files are created:
- Verify the
XCTOOL_TEST_ENV_LLVM_PROFILE_FILEenvironment variable is set correctly - Ensure the
code_coverage.buckconfigfile is being applied with--config-file - Check that your tests are actually running
Coverage Report Empty
If the coverage report shows no files:- Verify the test bundle path is correct
- Ensure the
.profdatafile was created successfully - Check that your source files are being compiled with coverage flags
Incorrect Coverage Percentages
If coverage seems too high or includes unwanted files:- Review your
-ignore-filename-regexpattern - Ensure you’re analyzing the correct binary
- Verify that coverage instrumentation is enabled during compilation