Product
Solutions
Resources
Company
Download Trial Book a Demo

Code Coverage for Swift Applications

Generate code coverage for Swift applications with RKTracer. Enable the rktracer tool on your Swift folders, rebuild and run your tests, and produce an HTML report.

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.

In short

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.

enable rktracer
rktracer -on foldername1 .swift -v
rktracer -on foldername2 .swift -v

Rebuild application

Clean and rebuild the application.

rebuild application
swift package clean

swift build

Run tests on the instrumented application

Run your tests against the instrumented application.

run tests
swift run

Generate code coverage reports

Run the rkresults command in the application root folder, where the rktracer folder was generated during instrumentation.

turn off 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

rktracer.config
ignore *.swift

instrument */power/* */core/* */keys/*

never */firmware_loader/* */lockdown/*

Generate coverage reports only for selected functions from three different files

rktracer.config
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.

Stuck on your build?

Open a support ticket and an engineer who knows your toolchain will help. You get a tracked ticket ID by email.