No such module error on @testable import means Xcode never produced your app’s Swift module, usually because the app target has a compile error that the test target’s failure is hiding. Comment out the test file, build the app target, fix the real errors, then confirm the name matches Product Module Name and that Enable Testability is Yes for Debug.Why this happens
@testable import MyApp is not a normal import. It asks the compiler to load the Swift module built from your app target and to expose its internal symbols. That module file only exists in the build products if the app target compiled successfully with testability enabled.
So the error almost always means one of two things:
- The app target never finished building, so no module was written. The test target fails first because it depends on that module, and its error hides the real compile error in your app code.
- The name after
importis not the module name Xcode actually produced.
A third, smaller group covers configuration problems: testability switched off for the active configuration, a test target with no host app, a platform mismatch, or a stale module left in DerivedData.
How to fix it
1. Surface the build error hiding behind it
This resolves most cases. The test file’s error is a symptom; the cause is a compile error elsewhere in the project.
- Comment out the entire test file that contains the failing
@testable import. - Select the app scheme and build with Cmd+B.
- Read the errors that now appear in the Issue Navigator and fix them.
- Uncomment the test file and run the tests again.
If the app builds cleanly and the error comes back, continue with the steps below.
2. Confirm the module name
The module name is not always the target name. Xcode derives it from the Product Module Name build setting, which defaults to $(PRODUCT_NAME:c99extidentifier). That transform replaces spaces, dashes and other characters that are not valid in a Swift identifier with underscores.
A target called My App therefore produces the module My_App, and My-App also becomes My_App. Writing @testable import MyApp then fails.
Read the real value from the command line:
xcodebuild -project MyApp.xcodeproj -target MyApp -showBuildSettings | grep PRODUCT_MODULE_NAME
Or select the app target, open Build Settings and search for “Product Module Name”. Use exactly that string:
@testable import My_App
If the module you are importing is a dependency rather than your own target, use a plain import instead. @testable only works for modules built with testability enabled, which third-party binaries usually are not.
3. Turn on Enable Testability for the configuration you test in
@testable requires the module to be compiled with -enable-testing. In Xcode this is the Enable Testability build setting (ENABLE_TESTABILITY), under Build Options.
- Select the app target.
- Open Build Settings and search for “Testability”.
- Set Enable Testability to Yes for Debug.
- Leave it No for Release. Testability widens the module interface and should not ship.
If your scheme or CI job builds the app in Release, @testable import fails there even though it works locally in Debug.
4. Give the test target a host app
A unit test bundle for an app needs a test host. Without one, the app module is not built as a dependency of the test target.
- Select the test target and open the General tab.
- Under Testing, set Host Application to your app.
- Check Build Phases → Target Dependencies includes the app target.
For a framework test target the host is usually None, and the framework must be linked to the test target instead. Framework targets also need Defines Module set to Yes, which is the default for frameworks but not for apps.
5. Clear stale build products
If the module was built once and the target was later renamed or its settings changed, Xcode can keep serving an old copy.
- Open the Product menu, hold Option and choose Clean Build Folder.
- Quit Xcode and remove the project’s DerivedData:
rm -rf ~/Library/Developer/Xcode/DerivedData/MyApp-*
- Reopen the project and build the app target before running tests.
6. Recreate the test target
This is the last resort, and it means re-adding your test files afterwards.
- Select the test target in the Project Navigator and delete it with the minus button.
- Add a new one with the plus button and choose iOS Unit Testing Bundle (or macOS Unit Testing Bundle).
- Set the host application and add your test files to the new target.
7. Check that the platforms match
An iOS app module cannot be imported by a macOS test bundle, and vice versa. Confirm the test target’s Supported Platforms and Base SDK match the app target’s. The same problem appears after a project migration that left the test target on a different SDK.
How to prevent it
- Build the app target with Cmd+B before running tests, so real compile errors appear first.
- Keep target names free of spaces and dashes, or remember the underscore substitution.
- Never turn off Enable Testability in Debug.
- Keep testability off in Release and make sure CI runs tests against Debug.
- Commit shared schemes with the Test action already configured, so the host app and test targets stay wired together.
- Delete DerivedData after renaming a target or changing its product name.
FAQ
Why does @testable import work in one project but not another?
The module name, the test host, or the build configuration differ. A project whose target is called MyApp, with a host app set and tests running in Debug, works; one whose target is called My App and has no host does not.
Can I use @testable import in a Release build?
Only if Enable Testability is Yes for Release, which is not recommended because it exposes internal symbols and enlarges the module. Test against Debug and keep Release free of testability.
What if the error says No such module 'XCTest' instead?
That is a different problem: the test target cannot find the XCTest framework, usually because its platform or SDK is wrong, or because the bundle was added as the wrong target type. Check the test target’s Base SDK and confirm it is a unit test bundle rather than an app target.
Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.