XCode and Code Coverage

Humble iOS Developer
iOS App Development
2 min readApr 11, 2015

If you don’t do it, it is because you don’t want to.

Edit: This post is outdated since WWDC2015.
Tune up for new posts, I’ll comment on the new Xcode coverage functionalities!
On the meantime, don’t even try to get coverage from the new UI tests, it won’t work on the first pre-release, it is a known issue.

Get in-line code coverage information

  1. Get XCoverage (if you don’t use Alcatraz, maybe it is time to consider)
  2. Follow easy instructions in their documentation
  3. Enjoy ☺

Get a global report

  1. Get lcov (if you don’t use Homebrew, maybe it is time to consider)
  2. Generate your awesome (but not very pretty) html report:

Some additional notes…

These might not be obvious at first, but might have been pretty useful to know in advance in my little adventure:

  1. ${OBJECT_FILE_DIR} (where .gcda, .gcno, .o… files reside) is at “/Users/<username>/Developer/Xcode/DerivedData/<appIdDir>/Build/Intermediates/<buildDir>/<targetDir>/<appNameDir>/Objects-normal/x86_64/” easy, right? I might even build a script to extract them!
  2. You might need to configure the Build Phase to accept paths with spaces by wrapping paths with quotes.

UIAutomation coverage!

But not only unit tests can benefit from coverage analysis. To do it, just edit the Pre-actions and Post-actions of a Run Scheme.

With this setting you will be able to analyze coverage of UIAutomation tests. Before doing it, just take into account the following (from QA1514):

[…Xcode will write out the coverage data to .gcda files when your application exits…].

But, beware that…

[…iOS applications do not exit. Therefore, iOS developers should temporarily modify their application to force coverage data to be written out to .gcda files. Use any of the following methods to do so:

  • Set the UIApplicationExitsOnSuspend key to YES in your Info.plist file and send your running application to the background by pressing the home button.
  • Add a call to __gcov_flush() in your application to force the coverage data to be written out to .gcda files. For instance, you may want to insert a call to __gcov_flush() in applicationDidEnterBackground: as shown in Listing 1 to write out coverage information every time the home button is pressed.]

--

--

Humble iOS Developer
iOS App Development

iOS developer eager to learn and share. @fbeeper’s alter ego.