How To Generate Code Coverage Reports In Xcode With Slather

Now that you know how to enable code coverage in Xcode, you may want to export the test results or generate a report you can share. Even though Xcode currently does not offer this option, nothing stops you from using the data Xcode generates.

Jenkins is a popular open source automation server and I have been looking for a solution to create an automated test report using the code coverage data generated by Xcode. Under the hood, Xcode uses LLVM to generate code coverage data. Even though the format is documented on the LLVM website, parsing the code coverage data is a different story.

Slather

A few months ago, I came across a gem that recently added support for the code coverage data Xcode generates. I am not joking when I write that Slather is a gem, a Ruby gem to be precise. You can install Slather through RubyGems by executing the following command:

gem install slather

Alternatively, you can add Slather to your project's Gemfile and run the bundle command.

Generating Reports

Before you can generate a code coverage report with Slather, you need to run your test suite. Slather uses the code coverage data Xcode collects when you run your test suite. Slather only parses the data generated by Xcode. It does not collect or generate the code coverage data itself.

As a test, download the project we created in the previous tutorial from GitHub and open it in Xcode. Code coverage should already be enabled for the Covered scheme.

Enabling Code Coverage

Run the test suite by choosing Test from the Product menu or by pressing Command + U. Even though Xcode collects a lot of code coverage data, only a fraction is displayed in the Reports Navigator. Xcode does not show what percentage of the code base is covered by the test suite, for example, a metric you can use to keep track of the test suite's coverage over time.

Fortunately, we can obtain this information through Slather. Open Terminal, navigate to the root of the project, and execute the following command:

slather coverage --scheme Covered --show ./Covered.xcodeproj/

Slather generates the following output for us:

Slathering...
Covered/AppDelegate.swift: 7 of 21 lines (33.33%)
Covered/Person.swift: 3 of 6 lines (50.00%)
Covered/ViewController.swift: 4 of 8 lines (50.00%)
Test Coverage: 40.00%
Slathered

As you can see, Slather gives us a more detailed report with metrics per file. You can also generate an HTML report by using the --html option.

A Code Coverage Report Generated by Slather

Slather Configuration

Complex projects require more configuration, but Slather has you covered. You can optionally add a .slather.yml file to the root of your project to further configure Slather. The extension of the configuration file hints at the format Slather expects, YAML.

In the example below, we specify the Xcode project, the scheme to use, and, more importantly, the service or output format Slather should use. We can optionally specify the output directory, the source directory, and files or folders Slather should ignore.

# .slather.yml

coverage_service: cobertura_xml
xcodeproj: ./Covered.xcodeproj
scheme: Covered
output_directory: ./test-reports
ignore:
  - "ThirdParty/*"

If you run slather from the root of the project, Slather reads the configuration of .slather.yml and uses it to generate a test report.

Services

Slather integrates with a number of popular services, such as Codecov, Coveralls, and Travis CI. It can also output the results of the code coverage report to several formats, such as HTML and the Cobertura format. I use the Cobertura format to integrate with Jenkins, for example.

What's Next?

I was very happy to discover that Slather enabled me to export Xcode's code coverage data to the Cobertura format. My hope is that Apple adds native support for exporting code coverage data in a future version of Xcode. That would make test results much more useful for more developers.

Questions? Leave them in the comments below or reach out to me on Twitter.