Integrate Xcode Code Coverage (xccov) into Jenkins

Why Xcode is Useful and How to Use it

Csaba

Csaba

Senior Software Engineer / Unit Testing Lead at Softvision
Csaba is a passionate mobile developer who started his iOS adventure in 2011 and has put his skills to work at Softvision since 2014. Lately he shifted his focus on Unit Testing and Continuous Integration leading efforts in that direction on his project.
Csaba

Latest posts by Csaba

Introduction:

Maybe you’ve already heard about the “xccov” tool released by Apple in Xcode 9.3. It’s marketed mostly as “Human readable code coverage report.”

You may ask how this would be useful, considering that Xcode already displays us Code Coverage information in a nicely done GUI? Let me explain it shortly:

  • If you are looking for Code Coverage means you’re into Unit Testing
  • If you’re into Unit Testing you want to see some fancynumbers/chartss how far you’ve got, what’s the percentage of the code you’re exercising by your tests and tell the world (or maybe just your team) what you’ve achieved
  • The best way to achieve that is to integrate it into your CI process!

To avoid confusion, it’s not something which hasn’t been done before! There are a handful of third-party scripts/ libraries which do the same, but if you’ve tried to use them, you know the struggle when a new Xcode appears and you need to wait for the third-party to prepare an update or try to fix it by yourself.

Now let’s dive into it…

As a preliminary step, you will need to setup your CI job to run your Unit Tests using the following xcodebuild option -derivedDataPath Build/ . This will put your DerivedData folder into your workspace under the Build folder. Why is this needed? Your Code Coverage data will be parsed by the xccov tool from .xccovreport and .xccovarchive files which are generated inside your DerivedData folder. Also, let’s not forget about the magic xcodebuild option -enableCodeCoverage YES in case your test target is not configured to gather coverage data.

Now, how to obtain that nice machine parsable Code Coverage data? After you run your Unit Tests following the aboves, run the following command in Terminal:

$ xcrun xccov view Build/Logs/Test/*.xccovreport –json which will provide you something like the following:

code

Note: Starting from Xcode 10, the above path for the coverage report is changed, so you will need to use $ xcrun xccov view Build/Logs/Test/*.xcresult/*_Test/*.xhttps://www.softvision.com/blog/our-experience-as-ios-developers-with-react-native/covreport –json.

Now let’s analyze it… The above JSON has a nesting depth of four. It means we need four data structures to represent it. What I’ve chosen is to convert it into a Cobertura XML format because there’s a Jenkins plugin for it.

How to parse..? Let’s write a script for it. A logical choice for the scripting language would be Swift. Why? Because the audience is mostly iOS / MacOS developers, familiar with the Swift programming language. Also, if you remember, Swift 4 introduced a nice feature which provides a simple way of parsing JSON data.

Let’s see how we can map our data:

That’s all, you have your data mapped into these structures!

The next step would be to generate a Cobertura XML format which you can pass to your Post build action plugin to display the data gathered by the above script.

The above function will generate a Cobertura XML by filtering targets (maybe you have multiple targets), packages (you don’t want to alternate your coverage data by Framework coverage, or other dependencies included). The rest of the details are specific to Cobertura XML format, to understand it better please see this link.

Before you start to analyze that it’s complexity is O(n3), I can assure you that it won’t be a bottleneck for your CI jobs, from my testing it doesn’t take more than a couple of seconds to generate the report.

Last, but not least, let’s see it in action:

screenshot

It also generates a detailed view of the Code Coverage inside each build containing your projects directory tree, and if you open a specific file, it even displays line coverage!

I’ve used Kingfisher third-party library as my sample app to show how it will look like:


There’s still room for improving the script linked above (see comment inside the code about line coverage), but hopefully, it suits your needs.

Conclusion:

Finally, there’s a Native solution to gather Code Coverage data for iOS / MacOS developers which can be nicely integrated into a CI environment! Even though in the future there could be breaking changes brought by Apple to this tool, updating a Swift script should be straightforward for iOS / MacOS developers.

Share This Article


Csaba

Csaba

Senior Software Engineer / Unit Testing Lead at Softvision
Csaba is a passionate mobile developer who started his iOS adventure in 2011 and has put his skills to work at Softvision since 2014. Lately he shifted his focus on Unit Testing and Continuous Integration leading efforts in that direction on his project.
Csaba

Latest posts by Csaba

No Comments

Post A Comment