Before Xcode 9.2, we can get formatted coverage data using https://github.com/SlatherOrg/slather, for example. The library formats llvm-cov.
But from Xcode 9.3, xccov is introduced officially. The command can run via xcrun.
xccov is a new command-line utility for inspecting the contents of Xcode coverage reports. It can be used to view coverage data in both human-readable and machine parseable format. To learn more, enter man xccov in Terminal. (37172926)
I tried the feature with https://github.com/KazuCocoa/test.examples and I put a simple Ruby gem script to get the target name and the line coverage.
Run
$ git clone https://github.com/KazuCocoa/test.examples && cd test.examples $ xcodebuild -workspace test.examples.xcworkspace -scheme test.examples -derivedDataPath Build/ -destination 'platform=iOS Simulator,OS=11.3,name=iPhone 7' -enableCodeCoverage YES clean build test CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO $ xcrun xccov view --only-targets --json Build/Logs/Test/*.xccovreport > result.json
Get JSON
We can get some kind of format.
# Output: https://gist.github.com/KazuCocoa/40eaa3ac9de5e52c1a3795c49657dd4b $ xcrun xccov view --json Build/Logs/Test/*.xccovreport | jq . # Output: https://gist.github.com/KazuCocoa/879554e02934d368f976959d3f8cec4b $ xcrun xccov view --only-targets --json Build/Logs/Test/*.xccovreport | jq .
Parse the JSON with Ruby
parsed = Xccov::Parse.new(file: './result.json') parsed.targets_line_coverage["test.examples.app"] #=> 0.35
Conclusion
From Xcode 9.3, we can get formatted coverage data using xcrun command. I’ve seen some scripts use Slather and Nokogiri to handle XML format and collect them. But the script will be more simple. Does this make you happy?
Great article! I thought the standard coverage report was a little too bland so I made this to make it look prettier 😀 https://github.com/mkchoi212/xcperfect Would love to hear what you think of it!
Thanks for the comment and the library! Pretty cool visualisation!