DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • How To Implement Code Reviews Into Your DevOps Practice

Trending

  • How to Use AWS Aurora Database for a Retail Point of Sale (POS) Transaction System
  • Introduction to Retrieval Augmented Generation (RAG)
  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • IoT and Cybersecurity: Addressing Data Privacy and Security Challenges
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. xccov: Xcode Code Coverage Report for Humans

xccov: Xcode Code Coverage Report for Humans

Learn about the native Apple developer tool xccov, which shipped with Xcode 9.3 and can be used to generate code coverage reports.

By 
Shashikant Jagtap user avatar
Shashikant Jagtap
·
Apr. 05, 18 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
13.3K Views

Join the DZone community and get the full member experience.

Join For Free

Apple has released new command line tool, xccov, with Xcode 9.3 for inspecting the contents of Xcode code coverage reports. Unfortunately, there isn't any web documentation yet, so we have to type man xccov in the terminal to get more information about this command line tool. This utility requires Xcode 9.3 and command line tools shipped with Xcode 9.3. With xccov, we can generate Xcode code coverage reports in the human-readable format as well as machine representable format like JSON without using third-party tools. In this post, we will explore how to generate and view Xcode code coverage reports using this new command line utility with a demo iOS app.

Mind the Name

The command line tool xccov is a native Apple developer tool shipped with Xcode 9.3. However, there are a few open-source tools with similar names that might confuse you.

  • It’s not xcov.

There is a Ruby library with name xcov to generate nice-looking code coverage reports, which can be used with tools like Fastlane and Danger.

  • It’s not hiroakit/xccov.

There is another pure Swift library to generate Xcode code coverage reports which is also called xccov, but is written by Hiroaki Endoh.

These libraries have similar names to the new utility launched by Apple, and coincidently, they are doing the same job, i.e. generating nice code coverage reports for Xcode. With the launch of Apple’s xccov, these libraries might not be needed.

Generating Code Coverage Reports

In order to explore xccov, let’s create a new iOS app with the "Tabbed App" template with Unit and UI Test targets and name it "XCCov-Demo." This will create the Xcode project scheme "XCCov-Demo." We can explicitly enable the code coverage for the scheme by editing the scheme and ticking the box "Code Coverage" from the "Test" action. We can filter targets as well if we don’t want to include coverage for UITests, as shown below:

Image title

Now we have enabled code coverage for our scheme. Once we build and test this scheme using the CMD+U button in Xcode, this will generate code coverage reports into the default derived data directory located at~/Library/Developer/Xcode/DerivedData and you will see the code coverage reports generated in the Logs/Test directory. However, for this demo, we will generate the derived data inside our project so that we can easily look at the reports.

Let’s build and test our app using the following xcodebuild command from the root of the project:

$ xcodebuild -project XCCov-Demo.xcodeproj/ -scheme XCCov-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 and generate the code coverage data at path Build/Logs/Test. We will see code coverage with an .xccovreport and .xccovarchive extension.

What’s Inside the Coverage Report

Inside the Logs/Test directory, there is a coverage report with the extension .xccovreport, and the coverage archive, with the extension .xccovarchive, respectively. As per the main page of this utility, “The coverage report contains line coverage percentages for each target, source file, and function/method that has coverage information. The coverage archive contains the raw execution counts for each file in the report.” However, they are not human-readable files; that’s the reason we need xccov to look inside these files and display the reports in a nice format.

Image title

Viewing Code Coverage Reports

Now that we have generated code coverage logs, we can use xccov to view the code coverage in a human-readable format. In order to access xccov, we have put it inside the $PATH, or we can easily access it with the xcrun command line utility. At the moment, we can achieve the following things with xccov:

  • View the code coverage reports from the terminal
  • Spit out the JSON from code coverage reports
  • List all the files for which code coverage has been generated
  • Look at the code coverage report for one specific file

Let’s explore the commands to achieve this using xccov.

View Report -default

We can view code coverage reports in the default format, which isn’t particularly great but as per Apple, it’s human-readable. In our demo project, we can generate the report using the following command:

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

View Report- JSON

The real power of xccov comes when it can generate code coverage reports in the JSON format. As per Apple’s documentation on the main page, it's a 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

We have just added the flag --json at the end of the command to get the JSON reports.

List All the Files

We can also list all the files that have code coverage data.

$ xcrun xccov view --file-list Build/Logs/Test/*.xccovarchive/

This will display all the project files with the absolute path.

Code Coverage for a Specific File

We can also see code coverage for a specific file, which displays line-by-line reports in some sort of symbols. Not sure how useful that would be, but it's there.

$ xcrun xccov view --file ~/Desktop/XCCov-Demo/XCCov-Demo/AppDelegate.swift  Build/Logs/Test/*.xccovarchive/

We have to give full path of the file and you will get reports in symbols.

Watch It in Action

Watch this GIF containing the features of xccov:

Image title

Source Code

There is source code with a fully-instructed README file available on GitHub at XCCov-Demo. Feel free to check out the project and try it yourself.

Conclusion

With Xcode 9.3, Apple shipped xccov to reduce the pain of using third-party tools to display Xcode code coverage reports in a nice format. The default format doesn’t look great or usable at the moment, but the real power comes when it generates JSON. Using the JSON format, we can hook these reports on Continuous Integration servers.

Code coverage XCode

Published at DZone with permission of Shashikant Jagtap, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Beyond Code Coverage: A Risk-Driven Revolution in Software Testing With Machine Learning
  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • How To Implement Code Reviews Into Your DevOps Practice

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!