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

106,994

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

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

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.

Share:
106,994

Related videos on Youtube

Suragch
Author by

Suragch

Read my story here: Programming was my god

Updated on May 03, 2022

Comments

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

    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?

  • Zaphod
    Zaphod almost 9 years
    Having 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
    Zaphod almost 9 years
    I 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
    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
    vkalit over 8 years
    There is no "Clean Build Folder..." button in "Product" options, so use hotkey "cmd + alt + shift + K".
  • Suragch
    Suragch over 8 years
    If 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
    Rajive Jain over 8 years
    I 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
    Rajive Jain over 8 years
    For 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
    Bartłomiej Semańczyk over 8 years
    @Zac @testable doesnt work with UITests. See this related question: stackoverflow.com/q/31769120/2725435
  • George Yacoub
    George Yacoub over 8 years
    Enable testability and Defines Module is what did the trick. I didn't need to change the target memberships for regular *.swift files.
  • Mikael
    Mikael over 8 years
    I 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
    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
    Mikael over 8 years
    I 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
    albogdano about 8 years
    Also 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
    f0rz about 8 years
    Go 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
    Steve Kuo over 7 years
    I have the same issue after upgrading to Xcode 8.1, I've tried everything mentioned here, no success. See stackoverflow.com/questions/41088484
  • onmyway133
    onmyway133 over 7 years
    Most of the case, it is about the product module name. Check space and _
  • David Corrado
    David Corrado about 7 years
    I 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
    Glen T about 7 years
    FYI 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
    pableiros over 6 years
    After doing that, I had to set Enable Bitcode to No on the Test Bundle.
  • Maryam Fekri
    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
    Maryam Fekri over 6 years
    Do 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
    atineoSE over 6 years
    It 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
    MoralCode over 6 years
    This 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
    beryllium almost 6 years
    be careful about - and _. One of my projects has minus - in the name, but the module has underscore _ instead
  • RolandasR
    RolandasR almost 6 years
    this might be more the case of changing simulators but this was worked for me also
  • abbood
    abbood over 5 years
    i used the "" idea, and got Expected identifier in import declaration
  • J. Doe
    J. Doe over 5 years
    Lol it works but how stupid from xcode that it doens't throw a different error
  • Przemysław Wrzesiński
    Przemysław Wrzesiński over 5 years
    You shouldn't add the application swift files to your test target, it will make their content duplicated when running tests.
  • Craig Fisher
    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
    palmi over 5 years
    One 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
    SmileBot almost 5 years
    definitely mistaken to add your files to the test target directly. this will bite you. correct your answer.
  • SmileBot
    SmileBot almost 5 years
    This is wrong. Never add your test files to your target.
  • Shubham Ojha
    Shubham Ojha almost 5 years
    Cleaning the build using option button did the cool job in case of me.
  • Wukerplank
    Wukerplank almost 5 years
    Adding 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
    alstr almost 5 years
    The 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
    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 for PRODUCT_MODULE_NAME whatever shows up there is what should go after @testable import
  • mushcraft
    mushcraft about 4 years
    all non-alphanumeric characters may also need to be replaced with the underscore. My target was in this format App (Dev), The testable module became App__Dev_
  • Luke
    Luke about 4 years
    Deleting and re-adding Unit Test target solved the issue for me.
  • ThomasW
    ThomasW almost 4 years
    I 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
    ThomasW almost 4 years
    Actually, it seems that if you don't include the @testable import Foo line, your unit tests should run correctly.
  • Alex Motor
    Alex Motor almost 4 years
    You mustn't add test classes to your application target.
  •  Dmitriy Greh
    Dmitriy Greh over 3 years
    THANK YOU SO MUCH! Guys, when you change configuration of your project, then it's changing module name of your test target!!!!!!!!!!!!!
  • Ricardo
    Ricardo about 3 years
    Thanks mate, you saved my day
  • mushcraft
    mushcraft over 2 years
    XCTest does not need to have Target membership was the winner, winner for me!
  • Gamma-Point
    Gamma-Point about 2 years
    Tried 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.