Xcode code coverage percentage from command line

Viewed 860

Xcode coverage percent

I can see code coverage percentage from Xcode UI. I can navigate to .xcresult file and Coverage.profdata file in DerivedData folder, but these files are not human-readable. How to extract code coverage percentage from these files?

2 Answers

Lior is on the right track. First build and test the app using the xcodebuild using the following command from the root of the project

$ xcodebuild -project XCCoverage-Demo.xcodeproj/ -scheme XCCoverage-Demo -derivedDataPath Build/ -destination 'platform=iOS Simulator,OS=11.3,name=iPhone 7' -enableCodeCoverage YES clean build test CODE_SIGN_IDENTITY="" CODE_SIGNING_REQUIRED=NO

This will dump all the DerivedData inside the Build directory. This will generate the code coverage data at path Build/Logs/Test. We will see code coverage with .xccovreport and .xccovarchive extension.

Then to view code coverage reports in the default format which isn’t particularly great but as per the Apple, it’s human readable. However we can generate the report using the following command

$ xcrun xccov view Build/Logs/Test/*.xccoveragereport

The real power of the xccov comes when it can generate the code coverage reports in the JSON format. As per Apple’s documentation on the man page, its machine representable format. We can hook these JSON results anywhere we like or build another tool on top of this. We can generate JSON reports using the following command

$ xcrun xccov view Build/Logs/Test/*.xccovreport --json

If you are using Xcode 11 or above, You can use the following command to inspect the contents from the command line.

xcrun xcresulttool /path/to/result/.xcresult

And also export the data as JSON

see this WWDC talk for more information

Related