egor.zhdan
egor.zhdan

Reputation: 4605

"Module was not compiled for testing" error when using Swift Package Manager

I created a Swift library with swift package init --type library and generated an Xcode project with swift package generate-xcodeproj.

Now I'm trying to run the Test scheme in Xcode. It prints following error:

Module '<title>' was not compiled for testing

However when I run swift build and swift test in terminal, it works fine.

I have ENABLE_TESTABILITY set to YES in all of the targets. I didn't change anything in the project except this. How can I make Xcode perform unit testing?

Upvotes: 12

Views: 6548

Answers (4)

MMise
MMise

Reputation: 129

I had the same problem where Xcode would show the Module 'Foo' was not compiled for testing, but I could still run the tests normally. The downside was that any code errors or warnings would not show up.

What solved this was to set Enable Testing Search Paths (ENABLE_TESTING_SEARCH_PATHS)to Yes.

Upvotes: 0

Jaminyah
Jaminyah

Reputation: 1

  1. Xcode -> Product -> Scheme -> Edit Scheme

  2. Select the Info tab.

  3. Set the following - Build Configuration: Debug Add a check mark to Debug executable

Tested with Xcode 12.4(12D4e) and iOS 14.1 deployment target.

Upvotes: -1

Sudhir Bhagat
Sudhir Bhagat

Reputation: 147

You need to set the "Enable Testability" to Yes in build setting over your "Main Target"

enter image description here

Upvotes: 11

Eneko Alonso
Eneko Alonso

Reputation: 19702

I was having this issue today, it seems like @testable cannot be used with projects generated by Swift Package Manager.

Removing @testable from my import statements solved this issue. Of course, this means we can only test the public interface of our modules.

Upvotes: 4

Related Questions