Generate Code Coverage from SPM Test Command on Linux

December 17, 2019

If you want to generate a code coverage file with the swift test command on Linux that can be uploaded to codecov.io, you only need to follow these simple steps:

Run your tests with code coverage enabled:

$ swift test --enable-code-coverage

This will create a binary file with coverage data in your debug build folder. This file format is not supported by codecov. Fortunately, every swift compiler installation comes with llvm. Therefore we can use the llvm-cov export command to translate the coverage data to the lcov format, which is supported by codecov.

Transform the default.profdata file to a file in the lcov format.

$ llvm-cov export -format="lcov" \ 
    .build/debug/{YOUR_PACKAGE_NAME}PackageTests.xctest \
    -instr-profile .build/debug/codecov/default.profdata > info.lcov

Now you only need to upload your newly created info.cov file to codecov. A full example of how to upload Swift code coverage from Linux can be found at GitHub action for swift-base64.