Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Flutter and app store releases

A unit test target cannot @testable import the app module

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

  • Applies to Xcode 26, Swift 6, XCTest
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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"
  2. 2Match what you see, then jump to its fix

    1. 1ENABLE_TESTABILITY = NO for the configuration that the tests buildThe module was not built with Enable TestabilityFix
    2. 2The 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. 3The code under test is a framework target and DEFINES_MODULE = NOA framework target does not define a moduleFix

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_TESTABILITY

2Fix

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

Did this fix it?

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 ModuleNameFromBuildSettings

3Verify

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

Did this fix it?

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_MODULE

2Fix

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

Did this fix it?

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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.