How to make SwiftUI Preview build the main target only without test targets?

Viewed 544

I just want to run the SwiftUI preview of my code, however, it's also building my unit tests. They have some errors in so I don't want them to be built. They're not usually built when running my Xcode project, so why is SwiftUI building them? Is there anyway to turn this off?

After a clean build, I refresh the preview and the status bar shows this:

enter image description here

And this is the scheme configuration:

enter image description here

2 Answers

According to this Apple Dev Foums post reply from a "Frameworks Engineer", it's a known issue and there's no fix other than deleting your test targets (!).

It's a relatively recent post (~1 month old as of now) so hopefully it'll be fixed soon.

I experienced the same problem, after several endless attempts, I found this simple solution that worked for me:

  • Removed the test project from the schema build in edit schema -> build
  • Disabled coverage gathering in edit schema -> tests

I recommend creating a new schema to use for SwiftUI previews only, in case you want to keep your test projects, so you can switch schema depending on what you want to do (SwiftUI or tests)

disabled test project builds disabled code coverage

Related