How do I show code coverage in Swift Packages?

3.2k Views Asked by At

On my M1 mac, using Xcode 13.3, I created a package and displayed the code coverage bar (Editor menu –> Code Coverage).

After running tests, there is no indication of code coverage at all in the source code.

How do I get code coverage when testing a package?

2

There are 2 best solutions below

5
On BEST ANSWER

The problem is that you didn't turn on Code Coverage for the library's scheme. Turn it on:

enter image description here

See the checkbox at the bottom: Code Coverage? Check that checkbox.

Now run the package test and coverage will be gathered. The best way to see it, in my experience, is actually in the report navigator:

enter image description here

Edit: In more modern Xcode (14.3 and later) it's simpler. Tests default to using test plans, so just turn on code coverage in the test plan:

enter image description here

1
On

As of (at least) Xcode 14, the former answer is no longer correct. For a Swift Package you need to enable this on the test plan.

Steps:

  1. Show Test Navigator (CMD + 6)
  2. Select the title, in the example: TestPlanExample and select "Edit Test Plan"

Select Edit Test Plan

  1. Select the tab "Configuration" and scroll down to "Code Coverage"
  2. Enable Code Coverage and either select "All targets" or select which targets you wish you generate code coverage for. Enable Code Coverage
  3. Save the testplan