How can I generate code coverage for Swift packages on Linux or OS X?

At the moment, I'm not aware of a tool that directly supports Linux. For what it's worth, we've still been happy with our code coverage solution that supports our web framework and HTTP server that runs on both Linux and macOS.

Our Swift Package Manager based project, Kitura, uses Travis CI for continuous integration. Each build will compile and run tests on both Linux and macOS. We also run a nightly build on Travis CI that generates coverage data, which is then uploaded to to codecov.io to generate reports like this.

Under the covers, the build script runs swift package generate-xcodeproj and then xcodebuild test with code coverage enabled. Code coverage only runs on the macOS build environment, which contains xcodebuild. Since our Linux and macOS code paths are very similar, the coverage statistics are good enough for our purposes, and we are still running tests independently for each OS.


This is actually possible by passing --enable-code-coverage to swift test

$ swift test --enable-code-coverage

That will generate an .xctest bundle in .build/x86_64-unknown-linux/debug/ and a profdata file in .build/x86_64-unknown-linux/debug/codecov/ on Linux which you are able to then pass along to llvm-cov to generate a report.

e.g.

$ llvm-cov report .build/x86_64-unknown-linux/debug/PredicatePackageTests.xctest -instr-profile=.build/x86_64-unknown-linux/debug/codecov/default.profdata -use-color

Which will generate a report like the following report:

report

See llvm-cov report --help for more information, it can even produce HTML reports.