"No such module" when using @testable in Xcode Unit tests

I recently updated to Xcode 7 beta 5. I tried adding a unit test to an earlier project, but I am getting the error message "No such module [myModuleName]" on the @testable import myModuleName line.

enter image description here

I tried

  • cleaning the project with Option Clean Build Folder
  • checking that "Enable Testability" (debug) was set to Yes in the Build Options
  • deleting the tests target and then re-adding the iOS Unit testing bundle

None of this worked for this project (but I have gotten testing to work in another project). Has anyone else had this problem and solved it?


Solution 1:

Please check your Module Name that you try to import with @testable import "ModuleName". The module name should be the same on Target->Build Settings-> Product Module Name

Solution 2:

The answer that worked for me

The answer was that I had some errors in my project that was making the build fail. (It was just your standard every day bug in the code.) After I fixed the errors and did another clean and build, it worked.

Note that these errors didn't show up at first. To get them to show up:

  • Comment out your entire Test file that is giving you the "No such module" error.
  • Try to run your project again.

If there are other errors, they should show up now. Fix them and then uncomment your Test file code. The "No such module" error was gone for me.


In case this doesn't solve the problem for other people, you can also try the following:

Clean the build folder

Open the Product menu, hold down Option, and click "Clean Build Folder..."

enter image description here

Make sure that Enable Testability is set to Yes

In the Project Navigator click your project name. Select Build Settings and scroll down to Build Options. Make sure that Enable Testability is Yes (for debug).

enter image description here

Delete and re-add your Tests target

If you have done the other things my guess is that you probably don't need to do this. But if you do, remember to save any Unit Tests that you have already written.

Click your project name in the Project Navigator. Then select your Tests target. Click the minus (-) button at the bottom to delete it.

enter image description here

Then click the plus (+) button and choose iOS Unit Testing Bundle to add it back again. As you can see, you can also add a UI Testing Bundle in the same way.

A few other ideas

  • Make sure that all required classes are members of your test target.
  • Make sure that you have added all the required libraries.
  • Make sure that the module name is written correctly (see this answer).

Or...

Leave a comment or answer below if you found something else that worked.

Related

  • How to do a Unit Test in Xcode
  • Xcode UI Test example

Solution 3:

The problem for me was the iOS deployment target of the tests was not set to be the same as the main target. So be sure to check this.

In your test target:

Build Settings -> iOS Deployment Target -> iOS<same as the target you are testing>

Solution 4:

So this is how I went about getting my code to work after trying all suggested solutions from prior suggestions.

  • I set 'Enable testability' to 'YES' in project's Build Settings
  • I also set 'Defines Module' to 'YES' in my project's Build Settings.
  • For the regular .swift file(s) within my project, say MyApp, I was going to write test cases for, I have both the main "MyApp" and the "MyAppUnitTests" Targets checked under Target Membership.
  • I then selected my unit test file(s), declared the '@testable import MyApp' at the top, beneath the 'import XCTest', and only checked the "MyAppUnitTests" under Target membership

And everything worked like charm. Hope this helps.

Solution 5:

One gotcha to watch for is that if your module name has a dash character in it - then you will have to refer to it with an underbar instead _. For some reason I suspected this might be an issue and it was indeed my issue.

eg. @testable import Ocean-Swift becomes @testable import Ocean_Swift

Just one other thing, if you do use the @testable syntax be sure to not include your production code in your test target. I've found this will cause inexplicable weirdness.