Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get code coverage for Xcode for my Objective-C app?

Tags:

I need code coverage for my iPhone app.

How do I get code coverage for Xcode 4?

like image 944
aryaxt Avatar asked May 01 '11 22:05

aryaxt


People also ask

How do I get code coverage for Xcode?

Enabling Code Coverage in Xcode Code coverage is enabled in the scheme editor. Click the Covered scheme and choose Edit Scheme.... Select Test on the left and check the checkbox Gather coverage data. That is it.

How do I get code coverage?

To calculate the code coverage percentage, simply use the following formula: Code Coverage Percentage = (Number of lines of code executed by a testing algorithm/Total number of lines of code in a system component) * 100.

How does Xcode measure test coverage?

Press Cmd+9 to open the report navigator, and look for “Coverage” under the most recent test run. This will show the code coverage data that was just collected – open the disclosure indicators so you can see inside User.

How do I configure code coverage?

Configure coveragePress Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Coverage. Define how the collected coverage data will be processed: Show options before applying coverage to the editor: show the Code Coverage dialog every time you run a new run configuration with code coverage.


2 Answers

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

  • http://atastypixel.com/blog/unit-testing-and-coverage-with-xcode/
  • http://milesdennis.blogspot.co.uk/2011/04/code-coverage-in-xcode-4.html
like image 134
user706638 Avatar answered Nov 24 '22 00:11

user706638


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" } 
like image 39
zekel Avatar answered Nov 24 '22 00:11

zekel