226

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?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393

35 Answers35

259

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

Voda Ion
  • 3,426
  • 2
  • 16
  • 18
  • 5
    Most of the case, it is about the product module name. Check `space` and `_` – onmyway133 Mar 02 '17 at 10:09
  • 6
    be careful about `-` and `_`. One of my projects has minus `-` in the name, but the module has underscore `_` instead – beryllium Aug 03 '18 at 12:01
  • 6
    i used the `""` idea, and got `Expected identifier in import declaration` – abbood Nov 16 '18 at 12:12
  • 4
    @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` – jonmecer Nov 17 '19 at 04:39
  • 1
    THANK YOU SO MUCH! Guys, when you change configuration of your project, then it's changing module name of your test target!!!!!!!!!!!!! –  Dmitriy Greh Nov 02 '20 at 14:59
133

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

Community
  • 1
  • 1
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 2
    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 Aug 23 '15 at 22:18
  • 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. – Zaphod Sep 14 '15 at 22:22
  • 7
    *** 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. – pauln Oct 09 '15 at 16:26
  • There is no "Clean Build Folder..." button in "Product" options, so use hotkey "cmd + alt + shift + K". – vkalit Nov 17 '15 at 21:35
  • 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. – Suragch Nov 18 '15 at 01:00
  • 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 https://realm.io/news/jorge-ortiz-unit-testing-swift-2/ – Rajive Jain Jan 10 '16 at 22:27
  • 2
    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. – Rajive Jain Jan 10 '16 at 22:32
  • @Zac `@testable` doesnt work with UITests. See this related question: http://stackoverflow.com/q/31769120/2725435 – Bartłomiej Semańczyk Jan 19 '16 at 15:18
  • 1
    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. – albogdano Apr 05 '16 at 10:36
  • 3
    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. – f0rz Apr 07 '16 at 13:28
  • I have the same issue after upgrading to Xcode 8.1, I've tried *everything* mentioned here, no success. See http://stackoverflow.com/questions/41088484 – Steve Kuo Dec 11 '16 at 17:07
  • 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 – David Corrado May 24 '17 at 21:40
  • 1
    FYI as per this post @testable doesn't work for logic tests. You need to run this in the App Host (see https://forums.developer.apple.com/thread/13118 ) – Glen T Jun 13 '17 at 22:37
  • +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. – Craig Fisher Jan 31 '19 at 17:56
  • Cleaning the build using option button did the cool job in case of me. – Shubham Ojha Jul 24 '19 at 08:57
  • 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. – Wukerplank Sep 12 '19 at 12:25
  • 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. – alstr Sep 12 '19 at 12:30
  • Deleting and re-adding Unit Test target solved the issue for me. – Luke May 15 '20 at 22:44
  • `Delete and re-add your Tests target` this works for me. – Suke Jun 27 '23 at 06:47
73

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>
Jess
  • 1,394
  • 12
  • 12
38

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.

Vick Swift
  • 2,945
  • 1
  • 15
  • 17
  • 14
    Enable testability and Defines Module is what did the trick. I didn't need to change the target memberships for regular *.swift files. – George Yacoub Mar 02 '16 at 14:50
  • 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 – Mikael Mar 18 '16 at 01:21
  • @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). – Vick Swift Mar 18 '16 at 11:31
  • 6
    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 – Mikael Mar 19 '16 at 04:29
  • 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. – atineoSE Nov 22 '17 at 17:19
  • 4
    You shouldn't add the application swift files to your test target, it will make their content duplicated when running tests. – Przemysław Wrzesiński Dec 11 '18 at 22:23
  • definitely mistaken to add your files to the test target directly. this will bite you. correct your answer. – SmileBot Jul 20 '19 at 17:43
  • You saved my day. It was the "Defines Module" that was set to NO... thanks – David Ansermot Dec 06 '22 at 10:25
21

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.

SmileBot
  • 19,393
  • 7
  • 65
  • 62
  • 1
    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_``` – mushcraft May 05 '20 at 08:59
15

This sounds to be an error with the build settings of both targets. You need to ensure that:

  • ENABLE_TESTABILITY equals Yes for both targets.
  • The PRODUCT_MODULE_NAME value of the test target should differ from the one of the application.
yageek
  • 4,115
  • 3
  • 30
  • 48
  • 2
    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 – MoralCode Dec 23 '17 at 19:20
15

For those who have scrolled until the last answer and still nothing worked, here is what did it for me after following all other answers advices. I am using Xcode 11:

What caused the issue in my case was that I changed my Product Name

  1. I changed my Product Name in the Build Settings of my main target to "New Name"
  2. I had to re-select the Host Application for my test target
  3. I didn't know that changing the product name would also change the Product Module Name, that is the one used for the module import in my test files. I changed my import as follows:

    @testable import New_Name

  4. It worked

I hope it helps

SwissMark
  • 1,041
  • 12
  • 21
12

After spending couple of days on this issues finally I make it to work with my project. Problem was in Bridging Header - path in Tests target can't be empty if you are using Bridging Header in your main targetBridging Header can't be empty!!!

Hope it will save some time for someone.

Igor P
  • 151
  • 3
  • 7
9

One other thing to check: If you have an Objective-C project, but are writing unit tests in Swift, make sure the main target uses at least one Swift file!


More info:

I was working on an Objective-C project, but wanted to write unit tests in Swift.

I added a Swift file to the main target to generate the necessary ProjectName-Bridging-Header.h file, wrote my tests and everything was working properly.

Later on I deleted the Swift file because I thought I didn't need it (all of the main target's code is in Objective-C... I was only writing tests in Swift).

I didn't notice a problem until later, after I did a "clean/clean build folder" and the "No Such Module" problem showed up. After some head scratching I added a new blank Swift file and the problem went away.

I've tested it multiple times with/without the Swift file, and it only works with it... so, I'll either need to leave the blank file in the project, convert some Objective-C into Swift, or add some new code to the project written in Swift.

Jim Rhoades
  • 3,310
  • 3
  • 34
  • 50
  • 1
    !! after 3 hours cleaning deleting Drieved data, cleaning, starting from scratch 3 times, I found your comment which solved my problem !!! Thanks !!!! – Maryam Fekri Nov 21 '17 at 19:07
  • 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 ? – Maryam Fekri Nov 21 '17 at 19:09
  • 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. https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_objective-c_into_swift – Pouria Almassi Feb 20 '19 at 01:59
  • 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: https://stackoverflow.com/q/62965954/211292 – ThomasW Jul 18 '20 at 07:40
  • Actually, it seems that if you don't include the `@testable import Foo` line, your unit tests should run correctly. – ThomasW Jul 19 '20 at 04:21
6

In my case , I had 3 issues. The first was that I had to specify the import path in :

Target -> Build Settings -> Swift Compiler - Search Paths -> Import Paths

The second was that I was using Pods and I had to import these pods to my tests as well using :

target 'MyAppTests' do
    inherit! :complete
end

The third one as that I was using a bridging header in my target , thus I had to specify the bridging header to be the same for the test.

el3ankaboot
  • 302
  • 2
  • 12
5

Make sure under the test scheme's build setting, the test target is in the list.

Beside the play button, select the test scheme, then Edit scheme..., go to the Build section, click plus + and select the target you want to test against.

In my case, we have an internal target that we develop with (a few minor differences) and after a merge, it was removed from the test config.

Edit test scheme

BrianHenryIE
  • 520
  • 5
  • 12
5

Here is yet another thing to check that is not listed. For me, it had something to do with my team, perhaps because our Team's Agent had not yet agreed to the latest License Agreement! Once I selected a different Team in my Target's General settings, AND then I specified a specific Deployment Target like 12.1 or 11.0, suddenly the "No Such Module" warning went away.

enter image description here

enter image description here

Dave Levy
  • 1,036
  • 13
  • 20
5

If you have some targets in your project - check your TARGETS in Module Name that you try to import with @testable import "TARGETSModuleName".

The module name should be the same on: Target -> Build Settings -> Product Module Name

For example:

TARGETSModuleName for Test

Nikolay
  • 127
  • 2
  • 3
4

I followed the steps above, which worked. However, my project had some more issues. I got this warning and I could not access classes from my main project to test in my test target.

Tests part of module - ignoring import

I found that your Test target Product Module Name (YourTestTarget -> Build Settings -> search for product module) cannot be the same name as your project name.

Product Module Name for test target cannot be the same name as your project name

Once I changed the Product Module Name for my test target everything worked.

enter image description here

Bryan Norden
  • 2,397
  • 18
  • 22
4

I tried all the answers here but the red flag would not go away. But I got it to work by just "running" an empty test regardless and it cleared up.

Things I would like make sure are done:

  • Host Application
  • @testable import "Module_name" (make sure the module name is correct)
  • Make sure you deployment target for the test is the same as the project
  • XCTest does not need to have Target membership
Alexander
  • 1,424
  • 18
  • 23
4

XCode 12.6 beta

I'm not sure what caused this issue for me but cleaning my build folder didn't sort it. Restarting XCode didn't sort out the issue either.

What worked for me was deleting this line: import XCTest, and then retyping it again.

LondonGuy
  • 10,778
  • 11
  • 79
  • 151
2

My issue was that the class i wanted to test was supposed to be in a separate module (API Client), but the class was actually a member of the app target and not the framework target. Changing the target membership of the class made the import error go away!

Maciej Swic
  • 11,139
  • 8
  • 52
  • 68
2

Environment: Xcode Version 9.0 (9A235)
Scenario: Testing an open-source framework.

I had the same problem: 'No such module'.

Solution:

  1. Select the Test target.
  2. Select Build Phases
  3. Add the framework to be tested via Link Binary...

enter image description here

Here's the test file: enter image description here

Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
2

I had this same issue. Cleaning the build folder and restarting Xcode did not work.

What did work for me was ensuring that the setting for "Build Active Architecture Only" of your test target and scheme matches the setting of your app's target and scheme.

Pouria Almassi
  • 1,580
  • 2
  • 17
  • 26
  • How do you do this? – ScottyBlades Nov 11 '22 at 06:46
  • @ScottyBlades Not so sure the scheme settings are important and no guarantee this was the culprit but... Getting to target settings: Select your project in the file navigator, in the items under TARGETS select your app target or test target. Filter for the setting. – Pouria Almassi Nov 17 '22 at 23:36
2

XCode 12 Development Beta 3

The error fixed itself after I've built the project for the first time.

Vlad Volkov
  • 183
  • 3
  • 5
2

This is what worked for me with Xcode 13.1:

In the Locations tab in Xcode Preferences > Locations I had defined a Custom location:

enter image description here

This caused not only unit tests to fail with the dreaded "No such module" error, but also also "Command CodeSign failed with a nonzero exit code" and other warnings and errors.

Changing the setting to Unique:

enter image description here

fixed all problems.

jaume
  • 381
  • 1
  • 5
  • 11
1

It could also be:

  • Not set the hosting application for your test target (in the general tab or TEST_HOST build setting)
  • Missing the app dependency in your Build Phase for your test target.
basvk
  • 4,437
  • 3
  • 29
  • 49
1

The problem occured for me on Xcode 14.3 with SwiftLint 0.51.0

If you use // swiftlint:disable all in the beginning of your import code and // swiftlint:enable all in the end(since SwiftLint recommends to do) of your test file, it could not detect the file, hence throwing the error. Not using // swiftlint:disable all and format my file solved the problem for me.

Ege Sucu
  • 160
  • 1
  • 12
0

For me the solution was to rename @testable import myproject_ios to @testable import myproject after I had updated product name of target myproject-ios in Build Settings/Packaging/Product Name/ from ${TARGET_NAME} to myproject.

Matthias
  • 11
  • 1
0

This was fixed for me when I changed the Deployment Target from 9.3 to 11.0.

General > Deployment Target > "11.0"

jacob_g
  • 674
  • 8
  • 21
0

If you are using xcodebuild and find this problem, consider adding in a workspace flag to the build command.

Changed This

$ xcodebuild -scheme PowToonsTests -destination 'name=iPhone X' test

To This

$ xcodebuild -workspace PowToons.xcworkspace -scheme PowToonsTests -destination 'name=iPhone X' test
Victor 'Chris' Cabral
  • 2,135
  • 1
  • 16
  • 33
0

In build settings test target, check the host testing, it takes the name set in PRODUCT_NAME. It is that name that you should use in test classes.

I recommand to not change PRODUCT_NAME (match name of the main target)

Sam
  • 97
  • 10
0

I think this may have happened because I deleted the example tests.

I removed the Unit test bundle then re-added it as shown in the pictures below and all was well again.

enter image description here

enter image description here

Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
  • 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. – Gamma-Point May 03 '22 at 00:57
0

CocoaPods recommends adding inherit! :search_paths to your test target like so:

target 'App' do
  target 'AppTests' do
    inherit! :search_paths
  end
end

Source: https://github.com/CocoaPods/CocoaPods/pull/8423#issue-244992565

Stijn
  • 858
  • 2
  • 12
  • 21
0

Tried all the solutions. Nothing worked. The Xcode build cli failed on a swift package module not found error.

error: no such module 'Apollo' import Apollo

Removing the Test targets and re-adding them worked for me.

Gamma-Point
  • 1,514
  • 13
  • 14
0

I changed the name of my project as I didn't want my original name for the app to show in the app store. This necessitated a change in my app host, which necessitated a change in the import I made.

Old name: QRCodeTarot

New name: Pointy Hat Tarot

This is the change I made for my host in my version control client.

enter image description here

This meant that in my tests, I needed to change the name of the import to the display name of my app. So even though my project was named QRCodeTarot, I needed to change the import to the display name.

So I went from import QRCodeReader to import Pointy_Hat_Tarot.

The recommendation to make your project name match your test import name is wrong.

The import name should match your project's display name, not your project name

ScottyBlades
  • 12,189
  • 5
  • 77
  • 85
-1

As described in this answer I was adding Swift tests to an Obj-C only project. The solution was to add a dummy Swift class, after which Xcode would prompt to add a bridging header, then removing the Swift class. All was fine after that.

Community
  • 1
  • 1
funkybro
  • 8,432
  • 6
  • 39
  • 52
-1

I addition to the other things listed, I had to add the file with the class I was trying to test to my compile sources for the unit test moduleenter image description here

Matt
  • 2,232
  • 8
  • 35
  • 64
-1

enter image description here

Click the MyAppTests.swift in the project navigator, and click the right panel, check your module in target Membership. It works in mine.

Firda Sahidi
  • 1,227
  • 1
  • 11
  • 22
-2

My solution is here.

Firstly OdeAlSwiftUITest.swift click, then check Project TargetName in target membership.

enter image description here

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
eyup cimen
  • 71
  • 1
  • 6