Suragch
Suragch

Reputation: 511538

"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

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?

Upvotes: 235

Views: 134806

Answers (30)

Ege Sucu
Ege Sucu

Reputation: 160

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.

Upvotes: 1

ScottyBlades
ScottyBlades

Reputation: 13963

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

Upvotes: -1

basvk
basvk

Reputation: 4546

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.

Upvotes: 1

Gamma-Point
Gamma-Point

Reputation: 1514

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.

Upvotes: 0

jaume
jaume

Reputation: 370

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.

Upvotes: 2

LondonGuy
LondonGuy

Reputation: 11098

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.

Upvotes: 4

Nikolay
Nikolay

Reputation: 145

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

Upvotes: 6

Vlad Volkov
Vlad Volkov

Reputation: 343

XCode 12 Development Beta 3

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

Upvotes: 2

Stijn
Stijn

Reputation: 858

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

Upvotes: 0

Alexander
Alexander

Reputation: 1484

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

Upvotes: 4

el3ankaboot
el3ankaboot

Reputation: 542

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.

Upvotes: 6

SwissMark
SwissMark

Reputation: 1143

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

Upvotes: 16

Firda Sahidi
Firda Sahidi

Reputation: 1281

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.

Upvotes: -1

Igor P
Igor P

Reputation: 161

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.

Upvotes: 13

Declan McKenna
Declan McKenna

Reputation: 4870

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

Upvotes: 0

Dave Levy
Dave Levy

Reputation: 1172

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

Upvotes: 5

Sam
Sam

Reputation: 97

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)

Upvotes: 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

Upvotes: 0

Bryan Norden
Bryan Norden

Reputation: 2537

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

Upvotes: 4

yageek
yageek

Reputation: 4455

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.

Upvotes: 15

eyup cimen
eyup cimen

Reputation: 69

My solution is here.

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

enter image description here

Upvotes: -3

jacob_g
jacob_g

Reputation: 779

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

General > Deployment Target > "11.0"

Upvotes: 0

Matthias
Matthias

Reputation: 11

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.

Upvotes: 0

Pouria Almassi
Pouria Almassi

Reputation: 1607

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.

Upvotes: 2

Jim Rhoades
Jim Rhoades

Reputation: 3320

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.

Upvotes: 9

Frederick C. Lee
Frederick C. Lee

Reputation: 9493

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

Upvotes: 2

Jess
Jess

Reputation: 1394

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>

Upvotes: 73

Matt
Matt

Reputation: 2350

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

Upvotes: -2

BrianHenryIE
BrianHenryIE

Reputation: 559

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

Upvotes: 5

funkybro
funkybro

Reputation: 8671

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.

Upvotes: -1

Related Questions