Is Big Cat Rescue as Bad as the Zoos in Tiger King?

EVERYONE has been asking my thoughts on Tiger King. I’m a wildlife biologist with 17 years of experience and a long personal history of doing research on animals in captivity. In fact, because it’s…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Reporting test coverage for Swift packages without Xcode

Learn how to report test coverage of Swift packages without using Xcode. By running tests in a Linux machine, you can integrate the process in your CI/CD system using a Docker container.

Adding Alamofire to your Xcode project via the Swift Package Manager.

Writing your own Swift packages is a good way of organizing your code base in reusable and easy to maintain libraries.

Think about an API that you access from more than one of your apps. Rewriting a Swift wrapper for each of your projects, without sharing code among them, is not only a waste of work, but also a way to add confusion to your team, which is now switching between different implementations of the same concept. It increases the chance of bugs in your code.

By moving the common code into a Swift package, it becomes the only source of truth for that code, making it easier to maintain.

You can work on a package directly in Xcode, and you can write tests and test your package, as you do with your apps. It is also possible to collect coverage data, using Xcode.

Testing a Swift package in Xcode.
Collecting code coverage while testing a Swift package in Xcode.

But you do not need Xcode, or even a mac, to work on and test a Swift package. In fact, the Swift ecosystem is open source, and it can be used on Linux to build and test Swift libraries and application.

In case you are not using Xcode, you need a different way to collect coverage information about your test suite, and that is exactly what I will show you in the rest of this article.

Test coverage information can be gathered easily. Open your terminal, or console, and change directory to the one containing your package. Then run swift test --enable-code-coverage. After tests are executed, you are greeted with an output like

Testing a Swift package from Terminal.

But where is the report about coverage? The swift test command does not specify it:

If you prefer a different output format, unfortunately the current version of the swift test command does not allow you to change it. But the tool it uses might allow it.

In fact, it does. If we run

The supported formats are: text (JSON), and lcov. We can run the same command swift test uses, and change the format:

You can find the correct paths for you when you run swift test --enable-code-coverage --verbose. You also might need to install a version of LLVM-cov.

which produces a 2003 looking website:

Code coverage report generated with lcov version 1.14

We use the result from the code coverage report to update our GitLab project, both for the merge request

A merge request in GitLab with a code coverage value

and for the project itself

The code coverage badge at the project page in GitLab

Add a comment

Related posts:

Tanzania Safari Trip for a Memorable Holiday in Africa

Planning to explore the hidden charm of Africa and want to begin the journey from the most charming country? Tanzania safari trip will be a wonderful experience for you that you will love to cherish…

Xoogler.co Growth Series with Gina Gotthilf

Gina Gotthilf joined the Xoogler community for a Growth Series event moderated by Holly Chen to speak on her time at Duolingo, Tumblr, and Mike Bloomberg’s presidential campaign. She provided a wide…

Why do we form a habit?

Knowing this will help understand how to stop bad habits and start new ones. Do you know that emotion plays a key role in habit formation? A positive emotion is perceived as a reward by the brain…