swiftunit-testingswiftuicode-coverageswift-package

How do I exclude SwiftUI Previews from code coverage in Swift packages?


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?


Solution

  • 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.