Code Coverage for Swift Applications

Code coverage for Swift language is a measurement, that can collect executed and non-executed code when tests are performed, such as unit test cases or during testing. It is a measure of the white box that finds the areas of the software not exercised by a series of test cases.  

As a baseline, we can use code coverage metrics to write new test cases to boost the coverage and thus determine calculated quantitative code coverage. 

In this 6mins video, you will see how to generate code coverage for Swift based application in command line interface using RKTracer Tool with 3 simple steps.

  1. Enable the RKTracer tool and rebuild the application.
  2. Test instrumented application and save coverage data.
  3. Generate code coverage reports

Code Coverage for Swift applications example

Identify the project folder that needs to be instrumented and generate coverage data. Please make sure the project is configured and you’re able to test it.

Enable the RKTracer tool

Instrument the selected folder with the rktracer tool. There might be multiple folders in the project, and
based on the requirement, you can do selected instrumentation.

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

Rebuild application

You need to clean the application and make sure there are no intermediate files.

swift package clean

swift build

Run Tests on instrumented application

Testing can be unit-testing or functional or integration testing of the application. 

swift run

Once your able to run the tests on instrumented application on host or server or device. The RKTracer tool will automatically save code coverage to rk-coverage.txt file.

Generate Code Coverage reports

Once you have the rk-coverage.txt file, you need to run the “rkresults” command to generate HTML reports. Ensure that you run the “rkresults” command in the application root folder or where you have the “rktracer” folder generated at the time of build/Instrumentation.

rkresults command will search coverage data file “rk-coverage.txt” and map with JSON files (generated during instrumentation) in the rktracer folder and generate the HTML reports. You can manually open the HTML report using index.html.

After generating code coverage, make sure to turn off(un-instrument) the RKTracer tool for the application that we instrument to create reports.

rktracer -off foldername .swift -v
rktracer -off foldername2 .swift -v

Enable coverage for selected files

Generate coverage reports  for the selected folders

C:\project\sound\drivers\base\power\

C:\project\sound\drivers\base\power\firmware_loader\

C:\project\sound\drivers\core\

C:\project\sound\security\keys\

C:\project\sound\security\lockdown\

Suppose you need code coverage for source files from three different folders, i.e., core, keys, power, and ignore coverage for folders firmware_loader and lockdown. Edit rktracer.config in the RKTracer installation folder and go to the end of the file add the following information. 

ignore *.swift

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

never */firmware_loader/* */lockdown/*

ignore *.swift = Ignores all swift source files from instrumentation

Instrument = Instrument source files from given folders

never = ignore selected folder  

Generate coverage reports only for selected functions from three different files.

fun_X() in source-file-X 

fun_Y() in source-file-Y

fun_Z() in source-file-Z

Suppose you need code coverage for selected functions from three different files. Edit rktracer.config in the RKTracer installation folder and go to the end of the file and set the following variables as shown below.  

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()

ignore *.swift = Ignore all swift programming source files

instrument *file-X.swift *file-Y.swift *file-Z.swift   = Instrument only these three source files.

function-ignore * = Then ignore all functions in the above three files.

function-instrument fun_X() fun_Y() fun_Z() = Don’t ignore these three functions from these three files from instrumentation.