"No such module" when using @testable in Xcode Unit tests
Solution 1
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..."
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).
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.
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
Solution 2
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 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.
Related videos on Youtube
Comments
-
Suragch about 2 years
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.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?
-
Shrawan over 7 years
-
Victor 'Chris' Cabral almost 6 yearsIf you are building from command line with xcodebuild make sure to add the -workspace flag for your build command. I solved this issue on my travis build that way.
-
John Rogers over 5 years
-
Zaphod almost 9 yearsHaving all the same problems here, with Xcode 7 beta 5. Unfortunately the steps above don't seem to solve it – the module is still regarding as, "no such module 'Utility'." The only difference from your screenshots is that I'm trying to get this working with the UI tests folder (GlimpulseUITests in my case). Does @testable not work with the UI test target perhaps?
-
Zaphod almost 9 yearsI did – in the end I deleted and re-added the test target, made sure that I had all the right target memberships, and it worked. By then I was using Xcode 7 GM. The root of the problem, I'm pretty sure, was that some required classes were not members of the UI test target. Make sure you include any libraries that you need as well.
-
pauln over 8 years*** IMPORTANT *** If you delete and re-add your Test Target, it will recreate a blank test template overwriting your existing tests. Be sure to save your test sources before doing this.
-
vkalit over 8 yearsThere is no "Clean Build Folder..." button in "Product" options, so use hotkey "cmd + alt + shift + K".
-
Suragch over 8 yearsIf you press the Option (alt) button when the Product menu is open, the "Clean Build Folder" menu item appears. The shortcut key combo you listed is a second way to do it.
-
Rajive Jain over 8 yearsI believe the @testable pattern specifically allows for importing and using all classe, properties & methods in your test target so "Make sure that all required classes are members of your test target." should not be required. See realm.io/news/jorge-ortiz-unit-testing-swift-2
-
Rajive Jain over 8 yearsFor me even my individual classes were not being shown while typing ... I finally did Product > Clean, restarted XCode. When it restarted, gave it a few seconds to complete indexing and then voila all my references showed up without having to include each class as a member of test target.
-
Bartłomiej Semańczyk over 8 years@Zac
@testable
doesnt work with UITests. See this related question: stackoverflow.com/q/31769120/2725435 -
George Yacoub over 8 yearsEnable testability and Defines Module is what did the trick. I didn't need to change the target memberships for regular *.swift files.
-
Mikael over 8 yearsI did all of the above steps but I still have the no such module error. my project is a swift- obj c mix though
-
Vick Swift over 8 years@Mikael, are you writing tests for only the Swift files in your Objc/Swift mixture code base? (I ask because, the last time I checked, I think the '@testable import' only worked for writing test cases for only Swift files even in Obj-c/Swift codebase mixture. It probably might have changed by now. Somebody correct me if I'm wrong here).
-
Mikael over 8 yearsI found my problem. It was because the Valid Architecture of my Test target was not the same as my main target's Valid Architecture configuration. Now it works. Btw, I m testing only Swift classes in my case, I didn't give it a try for Obj-c
-
albogdano about 8 yearsAlso if you haven't tried this, click on your missing framework on the left, then on the right select "Target Membership" and include it in your unit test target.
-
f0rz about 8 yearsGo to build settings of your main target -> "Product Module Name" and see if it match the module name you try to import in your test.
-
Steve Kuo over 7 yearsI have the same issue after upgrading to Xcode 8.1, I've tried everything mentioned here, no success. See stackoverflow.com/questions/41088484
-
onmyway133 over 7 yearsMost of the case, it is about the product module name. Check
space
and_
-
David Corrado about 7 yearsI found my issue to be related to these three things 1) Targets - > App Name - > Build Settings -> Product Name-> Update this to app name for all schemas 2) Targets -> App NameTests -> General -> Host Application-> Set this to your app for all schemas 3) Targets -> App NameTests -> Build Settings -> Build Active Architecture only-> Yes to all schemas
-
Glen T about 7 yearsFYI as per this post @testable doesn't work for logic tests. You need to run this in the App Host (see forums.developer.apple.com/thread/13118 )
-
pableiros over 6 yearsAfter doing that, I had to set
Enable Bitcode
toNo
on the Test Bundle. -
Maryam Fekri over 6 years!! after 3 hours cleaning deleting Drieved data, cleaning, starting from scratch 3 times, I found your comment which solved my problem !!! Thanks !!!!
-
Maryam Fekri over 6 yearsDo you know how to access the Objective-C classes in Swift test classes, cause I import the project module and there is no error with that, but it still doesn't recognize my Objective-C classes. should I do anyother thing ?
-
atineoSE over 6 yearsIt shouldn't be needed to set MyApp to the test target but I needed to do that temporarily to fix "has no member" issue in the test file.
-
MoralCode over 6 yearsThis worked for me. I was using the wrong module name. I was removing the space instead of adding an _. DOUBLE CHECK YOUR PRODUCT MODULE NAMES IN BUILD SETTINGS
-
beryllium almost 6 yearsbe careful about
-
and_
. One of my projects has minus-
in the name, but the module has underscore_
instead -
RolandasR almost 6 yearsthis might be more the case of changing simulators but this was worked for me also
-
abbood over 5 yearsi used the
""
idea, and gotExpected identifier in import declaration
-
J. Doe over 5 yearsLol it works but how stupid from xcode that it doens't throw a different error
-
Przemysław Wrzesiński over 5 yearsYou shouldn't add the application swift files to your test target, it will make their content duplicated when running tests.
-
Craig Fisher over 5 years+1 The problem for me occured when I rant a test on the release build where I disabled testability. Even when I switched back to a debug version, the error persisted. As mentioned, I commented out the all the code in the file that was producing the error, switched to another test class and ran one of the test (that I didn't run on the release target). Once that ran, I went back to my failing test class, uncommented and built. After running that class the error disappeared. Silly little xcode quirk is all it was for me.
-
palmi over 5 yearsOne additional point I'd like to add, though my mostly ObjC project does have at least one Swift file there was no bridging header for my target. Following the instructions at the following link under the heading "Import Code Within an App Target" resolved being able to access ObjC classes from Swift tests. developer.apple.com/documentation/swift/…
-
SmileBot almost 5 yearsdefinitely mistaken to add your files to the test target directly. this will bite you. correct your answer.
-
SmileBot almost 5 yearsThis is wrong. Never add your test files to your target.
-
Shubham Ojha almost 5 yearsCleaning the build using option button did the cool job in case of me.
-
Wukerplank almost 5 yearsAdding another test target and moving the files there worked for me. It makes no sense, the test targets are configured absolutely identically. Xcode works in mysterious ways.
-
alstr almost 5 yearsThe problem occurred for me after cleaning the build folder. Cleaning it again didn't help. I copied my tests to the clipboard, did a Git revert on the file, pasted the tests back, and the error went away. Why? No idea. But it was the only thing that worked out of a few things.
-
jonmecer over 4 years@onmyway133 was super close. In my case, I had a different product module name from my project name. To find your module name go to
Build Settings
select your project (not the test or UI test) then search forPRODUCT_MODULE_NAME
whatever shows up there is what should go after@testable import
-
mushcraft about 4 yearsall non-alphanumeric characters may also need to be replaced with the underscore. My target was in this format
App (Dev)
, The testable module becameApp__Dev_
-
Luke about 4 yearsDeleting and re-adding Unit Test target solved the issue for me.
-
ThomasW almost 4 yearsI ran into this issue. However, I'm interested in finding a way to avoid including the Swift file if possible, so I asked the question here: stackoverflow.com/q/62965954/211292
-
ThomasW almost 4 yearsActually, it seems that if you don't include the
@testable import Foo
line, your unit tests should run correctly. -
Alex Motor almost 4 yearsYou mustn't add test classes to your application target.
-
Dmitriy Greh over 3 yearsTHANK YOU SO MUCH! Guys, when you change configuration of your project, then it's changing module name of your test target!!!!!!!!!!!!!
-
Ricardo about 3 yearsThanks mate, you saved my day
-
mushcraft over 2 years
XCTest does not need to have Target membership
was the winner, winner for me! -
Gamma-Point about 2 yearsTried all the solutions. Nothing worked. The Xcode build cli failed on a swift package module not found error. Removing the Test targets and re-adding them worked for me.