Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am looking for a way to generate code coverage for a Swift package created using Swift Package Manager. On OS X I can achieve this using Xcode after running swift package generate-xcodeproj and running the test suite under Xcode at which point I have the standard coverage tools available to me.

Are there any tools out in the wild for that allow this to happen on OS X and Linux?

like image 363
marcus.ramsden Avatar asked Nov 25 '16 01:11

marcus.ramsden


People also ask

How do I enable code coverage in Swift?

To enable code coverage, click the scheme editor in the toolbar. Select the CodecovDemo scheme and choose Edit Scheme. Select the Test action on the left. Check the Code Coverage box to gather coverage data.

How do I run code coverage in Xcode?

Enabling Code Coverage in Xcode Code coverage is enabled in the scheme editor. Click the Covered scheme and choose Edit Scheme.... Select Test on the left and check the checkbox Gather coverage data. That is it.

Where is code coverage in Xcode 13?

You can find it under the Reports navigator. (View menu > Navigators > Reports or ⌘ - command + 9 ). After you open it, under the latest Test report, you should find a Coverage report, click on that, and it will contain the coverage information of that test run.


Video Answer


1 Answers

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.

like image 123
Oliver Atkinson Avatar answered Oct 07 '22 10:10

Oliver Atkinson