CoverStory on the iPhone

Since Kanchoo is getting to be a serious app, it’s time to make rewrite, refactor, and to adopt automated testing. How do you know whether your tests cover all possible cases? That’s hard, but you can at least verify that all your code is covered. Yes, today we’re talking about code coverage.

CoverStory is an awesome application for inspecting GCC’s gcov output. As you would expect from GCC, setting up gcov is easy and obscure. You need to:

  • Add “-lgcov” to Other Linker Flags.
  • Set GCC_GENERATE_TEST_COVERAGE_FILES to YES.
  • Set GCC_INSTRUMENT_PROGRAM_FLOW_ARCS to YES.
  • Run your app, then open the “Objects-normal” folder in CoverStory.

Let’s go over these steps in more detail. In Xcode, get info on the target you want to cover. Switch to the Build tab. Double click on the value of the “Other Linker Flags” setting.


Add “-lgcov”. OK. Now add two user-defined settings: GCC_GENERATE_TEST_COVERAGE_FILES and GCC_INSTRUMENT_PROGRAM_FLOW_ARCS. Set the value of both to YES.


Build and run. Now find the Objects-normal folder. The path is something like:

  • CoverageExample/build/CoverageExample.build/Debug-iphonesimulator/CoverageExample.build/Objects-normal

Or in general:

  • ${PROJECT_DIR}/build/${PRODUCT_NAME}.build/${CONFIGURATION}-${PLATFORM_NAME}/${PRODUCT_NAME}.build/Objects-normal

Open the Objects-normal folder in CoverStory: drag, drop, see the report, a summary showing the percent coverage of each file with a detail view showing the number of hits for each line in the gutter. When the hit count is zero, the line is highlighted in red.

Sometime lines of code were never meant to run: inconceivable, non-feasible. Mark these lines with COV_NF_LINE comments or mark a block with COV_NF_START followed by COV_NF_END. If non-feasible lines do get executed, CoverStory will warn you.

May your code be ever covered and your tests perpetually passing.