Xcode code coverage

Code coverage tools are a useful way to find untested code in a project but getting them to work with Xcode was a real pain. Xcode 7 now has code coverage fully integrated making it simple to start finding untested code.

Scheme Configuration

To start using code coverage add it to the Xcode scheme (Product > Scheme > Edit Scheme…). You will find it hidden in the Info tab of the Test action. If you have not already done so you may also want to add the Address Sanitizer in the Diagnostics tab.

Enable code coverage

Viewing the Results

Assuming your project already has some unit or user interface tests you can find the code coverage results of a test run in the report navigator.

Report navigator

Xcode breaks down the coverage results by file and method. In this case my project only has unit tests so I am not surprised the single view controller and app delegate have poor coverage. What is a surprise is that I have some methods that my unit tests are not even reaching.

Code coverage results

If you click on one of the methods you drill down into the code to see which paths are missing coverage. The numbers in the right hand gutter are counts of how often the tests executed that branch. Branches that were never executed show up in red making it easy to scroll down to the untested code and then write new tests to close the gap.

Code coverage results

Code Coverage with Xcode Server

When you use code coverage with an Xcode server bot you get some more details in the log:

Xcode server

Xcode server shows the code coverage results for each tested device. It also highlights differences in test coverage between devices with an orange bar.

Note that Xcode 7 is still beta software at time of writing so there are some rough edges (such as showing orange bars when there are no device differences).

Further Reading

Never miss a post!

iOS Size Classes Cheat Sheet

Subscribe and get my free iOS Size Classes Cheat Sheet

Success! Now check your email to confirm your subscription and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
No time to watch WWDC videos?

Sign up to get my iOS posts direct to your inbox and I will send you a free PDF of my iOS Size Classes Cheat Sheet.

OK! Check your inbox (or spam folder) for an email to confirm your details and download your free guide to iOS Size Classes.

There was an error submitting your subscription. Please try again.

Unsubscribe at any time.
Archives Categories