22

I need code coverage for my iPhone app.

How do I get code coverage for Xcode 4?

Cœur
  • 37,241
  • 25
  • 195
  • 267
aryaxt
  • 76,198
  • 92
  • 293
  • 442
  • Clang/LLVM don’t have support for gcov and don’t provide code coverage capabilities. You might be able to use gcov with GCC depending on whether your program builds with GCC. See [this bug report at LLVM](http://llvm.org/bugs/show_bug.cgi?id=8030) and consider [filing a feature request radar](http://bugreport.apple.com), too. –  May 01 '11 at 23:03
  • You have set UIApplicationExitsOnSusspend to YES in your info.plist – aryaxt Jun 24 '11 at 13:41
  • 1
    @Bavarious: According to your bugreport, clang supports coverage now? – Johannes Rudolph Jul 07 '11 at 13:32
  • 1
    @Joh Yup, nlewycky has added gcov support in LLVM trunk, and nope, the bug report isn’t mine. ;-) I’m not sure if/when Apple will make it available via Xcode, though. You might be able to use it if you build LLVM locally. –  Jul 07 '11 at 15:08

2 Answers2

9

These steps will help.

  1. Create a new build configuration (‘Coverage’), duplicated from the ‘Debug’ configuration.

  2. Open up build settings for the main target, make sure your new configuration is selected, and:

    Enable “Generate Test Coverage Files”
    Enable “Instrument Program Flow”
    Add “-lgcov” to “Other Linker Flags”
    
  3. Compile application with Coverage mode.

  4. Check .gcno files from your application bundle folder.

    Coverage-iphonesimulator/applicationname.build/Objects-normal

    open .gcno files with CoverStory. Download CoverStory from
    http://code.google.com/p/coverstory/downloads/list

Reference Sites

Miles D
  • 7,960
  • 5
  • 34
  • 35
user706638
  • 183
  • 4
3

I couldn't find a good example of this, so hopefully this will help someone else.

If you want to generate HTML from your code coverage (once you get your .gcda files generated), you can install lcov and use these commands:

function generate-codecoverage-html() {
    if [[ $1 == "-h" || ! $# -eq 2 ]]; then
        echo "    usage: $0 path/to/codecoverage/dir/ path/to/htmldir/"
        return
    fi

    timestamp=$(date)
    tmpfile="/tmp/codecoverage.info-$date"
    lcov --no-checksum --directory "$1" --capture --output-file "$tmpfile"
    genhtml --output-directory  "$2" "$tmpfile"
}
zekel
  • 9,227
  • 10
  • 65
  • 96