A unit test target cannot @testable import the app module
No such module 'MyApp'Short answer
A test target can only @testable import a module that was compiled with Enable Testability, under the name the build setting PRODUCT_MODULE_NAME gives it. Check which configuration the tests build, whether the app's module name differs from its product name, and for frameworks whether Defines Module is on.
Fix it fast
1Find out which cause you have
Print the module name and testability setting that the app target really uses in the configuration the tests build.
xcodebuild -project MyApp.xcodeproj -target MyApp -configuration Debug -showBuildSettings | grep -E "PRODUCT_MODULE_NAME|ENABLE_TESTABILITY|DEFINES_MODULE"2Match what you see, then jump to its fix
- 1
ENABLE_TESTABILITY = NO for the configuration that the tests buildThe module was not built with Enable TestabilityFix - 2
The app's product name has spaces or symbols, or PRODUCT_MODULE_NAME was changedThe import uses a name that is not the module nameFix - 3
The code under test is a framework target and DEFINES_MODULE = NOA framework target does not define a moduleFix
- 1
What does the error look like?
No such module 'MyApp'@testable import MyApp
Cause 1 of 3
The module was not built with Enable Testability
1Check
Apple says that to reach internal declarations from test code you compile the module with Enable Testability and add @testable to the import. The setting passes -enable-testing to the Swift compiler.
xcodebuild -project MyApp.xcodeproj -target MyApp -configuration Release -showBuildSettings | grep ENABLE_TESTABILITY2Fix
Run tests with the Debug configuration, or set Enable Testability to Yes for the configuration the test action builds. Do not ship that setting in the release build.
xcodebuild test -project MyApp.xcodeproj -scheme MyApp -configuration Debug -destination 'platform=iOS Simulator,name=iPhone 16'3Verify
The test target compiles and the @testable import line has no error.
xcodebuild build-for-testing -project MyApp.xcodeproj -scheme MyApp -destination 'platform=iOS Simulator,name=iPhone 16'Confirmed by developer.apple.com
Cause 2 of 3
The import uses a name that is not the module name
1Check
PRODUCT_MODULE_NAME is the name used to import the module in source files, and it must be a valid identifier. A product called My App does not import as My App.
xcodebuild -project MyApp.xcodeproj -target MyApp -showBuildSettings | grep -E "PRODUCT_NAME|PRODUCT_MODULE_NAME"2Fix
Write the import with the value PRODUCT_MODULE_NAME prints.
// use exactly the value PRODUCT_MODULE_NAME printed
@testable import ModuleNameFromBuildSettings3Verify
Build the test target again; the error is gone.
xcodebuild build-for-testing -project MyApp.xcodeproj -scheme MyApp -destination 'platform=iOS Simulator,name=iPhone 16'Confirmed by developer.apple.com
Cause 3 of 3
A framework target does not define a module
1Check
Apple documents Defines Module as the setting that treats a product as defining its own module and allows it to be imported as a module.
xcodebuild -project MyApp.xcodeproj -target MyFramework -showBuildSettings | grep DEFINES_MODULE2Fix
Set Defines Module to Yes on the framework target and build it again.
# Build Settings > Packaging > Defines Module = Yes (DEFINES_MODULE = YES)3Verify
The test target imports the framework without the error.
xcodebuild build-for-testing -project MyApp.xcodeproj -scheme MyFramework -destination 'platform=iOS Simulator,name=iPhone 16'Confirmed by developer.apple.com
How do I stop it coming back?
- Run tests in the Debug configuration, which is what Enable Testability is meant for.
- Keep product names free of spaces so the module name matches what you read.
- Check ENABLE_TESTABILITY in CI with showBuildSettings before blaming the code.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Mobile apps
Swift cannot import a module that is installed in the project
No such module 'Flutter'
The compiler for this target cannot see the module, even though the code exists on disk.
4 causes, each with a fixMobile apps
UserDefaults logs 'Failed to read values in CFPrefsPlistSource' for an app group
Settings saved by the app are not visible to its extension, widget or sibling app that share the group
This log comes from the preferences system when UserDefaults is opened for an app group's suite.
3 causes, each with a fixMobile apps
iOS app crashes: Google Maps SDK must be initialized via provideAPIKey
Google Maps SDK for iOS must be initialized via [GMSServices provideAPIKey:...] prior to use
The Maps SDK throws this when a Maps object is created before GMSServices.provideAPIKey has run.
3 causes, each with a fixMobile apps
UIKit app crashes because it cannot find the storyboard
Could not find a storyboard named 'Main' in bundle NSBundle
UIStoryboard throws this exception when no storyboard file with that name exists in the bundle it searches.
3 causes, each with a fixMobile apps
Firebase on iOS asks for the FCM token before APNs has given one
Be sure to re-retrieve the FCM token once the APNS device token is set.
The Firebase Messaging SDK needs the APNs device token from iOS before it hands out an FCM token.
3 causes, each with a fixMobile apps
A test push from the Firebase console never shows up on the iPhone
didReceiveRegistrationToken prints a token, yet no notification arrives
When a console message is accepted but the iPhone stays silent, the break is between Firebase and APNs or inside the app.
4 causes, each with a fix