0

I have found solutions explaining how to exclude code coverage for SwiftUI projects, but I have failed to find anything for SwiftUI Packages. My Swift package includes views (and previews) for use in other projects. But my coverage is badly skewed because it includes the previews in the unit testing.

The solution for Projects create a new testing configuration and exclude the previews for the testing configuration name. But I can't find a way to create this in a package.

Is there a way to exclude previews from the code coverage when unit testing Swift Packages?

2 Answers 2

1

It turns out in later versions of iOS the #Preview("name") { } can be used to create previews that are not included in unit testing coverage. Just covert the old PreviewProvider to #Preview macros.

Sign up to request clarification or add additional context in comments.

Comments

-1
  • Open your Swift package in Xcode.

    • In Xcode, navigate to the "Edit Scheme" settings for your project: Click on your project in the Project Navigator.
    • Select the scheme for which you want to configure code coverage.
    • Choose "Edit Scheme" from the scheme dropdown or use the keyboard shortcut ⌘<.
  • In the "Edit Scheme" window, select the "Test" action on the left sidebar.

  • In the "Info" tab, you'll see a section for "Code Coverage." You can configure code coverage options here. To exclude SwiftUI Previews, you can use the "Targets" option:

    • Deselect the checkbox for the targets related to SwiftUI Previews. This will exclude those targets from code coverage.
  • Close the "Edit Scheme" window to save your changes.

  • Run your tests as you normally would, and code coverage will be calculated with the excluded SwiftUI Preview targets.

1 Comment

I don't have a project, I have a package. Neither do not have a Project Navigator, but only a Package.swift file. There are no Schemes in that file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.