Code coverage for the Swift language measures executed and non-executed code during testing, such as unit test cases. It identifies areas of software not exercised by a test case series. Code coverage metrics establish baselines for writing new test cases to boost coverage and determine quantitative metrics.
Generate code coverage for Swift applications via the command line using the RKTracer tool in three steps: enable RKTracer and rebuild, test the instrumented application and save the coverage data, then generate the reports.
Code coverage for Swift applications example
Identify the project folder requiring instrumentation and generate coverage data, ensuring the project is configured and testable.
Enable the RKTracer tool
Run the rktracer -on command on the Swift folders you want to instrument.
rktracer -on foldername1 .swift -v rktracer -on foldername2 .swift -v
Rebuild application
Clean and rebuild the application.
swift package clean swift build
Run tests on the instrumented application
Run your tests against the instrumented application.
swift run
Generate code coverage reports
Run the rkresults command in the application root folder, where the rktracer folder was generated during instrumentation.
rktracer -off foldername .swift -v rktracer -off foldername2 .swift -v
Enable coverage for selected files
You can scope coverage to specific folders, files, or functions by editing rktracer.config.
Generate coverage reports for the selected folders
ignore *.swift instrument */power/* */core/* */keys/* never */firmware_loader/* */lockdown/*
Generate coverage reports only for selected functions from three different files
ignore *.swift instrument *source-file-X.swift *source-file-Y.swift *source-file-Z.swift function-ignore * function-instrument fun_X() fun_Y() fun_Z()
Next steps
Once your Swift application reports coverage, the same RKTracer workflow applies across other build systems and languages. Browse the rest of the documentation, or read more about how RKTracer measures coverage through MC/DC.