I recommend reading iOS Unit Test Automation — Unit Test Statistics first.

All scripts covered in this article are hosted in the GitHub project UnitTestParser. Demo project: UnitTestDemo

Statistics on overall code coverage and code coverage data for each target.

Parse code coverage files

You can use xcCOv, an official command line tool provided by Apple, to parse code coverage and obtain the overall code coverage and code coverage of each module.

xcrun xccov view --report --json #{xcresult_path} > #{json_path}
Copy the code

Once you have the JSON file, you can parse the JSON file to get code coverage. The UnitTestParser project provides target.rb to parse overall and modular code coverage.

# The incoming file is a JSON file parsed using 'xccov'
ruby targetCoverage.rb --cov-json-path=path/to/json_file --output-file=path/to/output_file
Copy the code

For example:

╰─± Ruby targetcoverage. rb --cov-json-path=result.json --output-file=result.htmlCopy the code

We can get the following result:

Target Number of Executable lines of code Coverage Number of executable lines of code Coverage All 424 313 73.8% Demotest.xctest 223 217 97.3% Demo.app 201 96 47.7%Copy the code