Generating a code coverage report for iOS and OSX projects requires a mish-mash of tools and steps.
Here we've glued them all together into something that (hopefully) just works. All it took was a
little Java, some Perl, a few shell scripts, a couple of Ruby Gems . . . and now you can:
- Generate a report with minimal steps.
- Output to the terminal as well as produce a detailed report in build-server friendly HTML
- Use the optional coverage checker, for example in CI builds to check minimum test coverage -
failing the build if coverage falls below the required amount.
- Use it for free for both commercial and open-source projects!
Swift? Sorry, no dice
As yet there's no way to generate code coverage reports for Swift. If, like us, you'd like to see
this implemented by Apple, please
vote on this issue.
The script itself can be run remotely, but we'll first need to install dependencies . . .
$ brew install groovy
$ brew install lcov
$ sudo port install groovy
$ sudo port install lcov
NB: Xcode 7.x requires lcov 1.12, or no coverage will be
produced. If you haven't updated your Brew or MacPorts sources for a while, please do so or you might
end up with lcov 1.10.
Set your main App target to produce test coverage output (debug mode only).
Set your main App target to instrument program flow (debug mode only).
FROM YOUR IDE
Run your tests from Xcode or AppCode, and then produce a coverage report with:
groovy http://frankencover.it/with -source-dir MyProject/Classes
FROM A BUILD SERVER OR LOCAL CMD-LINE
Create a build script, as follows:
# First Run Tests
xcodebuild test -workspace MyProject.xcworkspace/ \
-scheme 'MyProject' \
-configuration Debug \
-destination 'platform=iOS Simulator,name=iPhone 5s,OS=8.1' | xcpretty -c
#Above we're piping output through xcpretty, which is not required
#but very nice! (gem install xcpretty)
# Now Produce Test Coverage Report
groovy http://frankencover.it/with -source-dir MyProject/Classes \
#We set required coverage to 85% - build fails if coverage
#falls below this value.
. . . and run it with
$ sh build.sh