'Module was not compiled for testing' when using @testable

Solution 1:

In your main target you need to set the Enable Testability build option to Yes.

As per the comment by @earnshavian below, this should only be used on debug builds as per apple release notes: "The Enable Testability build setting should be used only in your Debug configuration, because it prohibits optimizations that depend on not exporting internal symbols from the app or framework" https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW326

Solution 2:

In my case I used a custom build configuration for testing (called Test) and also cocoapods as a dependency manager

I had to add the following lines to the end of my Podfile to enable testability

post_install do |installer|
    installer.pods_project.targets.each do |target|
        target.build_configurations.each do |config|
            if config.name == 'Test'
                config.build_settings['ENABLE_TESTABILITY'] = 'YES'
            end
        end
    end
end

By default cocoapods sets ENABLE_TESTABILITY to YES only for Debug builds

Solution 3:

Make sure that you properly set your checkboxes under your app scheme. You SHOULD UNCHECK your test targets for Archive Build.

enter image description here

Solution 4:

For those of you who are experiencing this only upon running Xcode Profiler: switch profile build configuration in your scheme management to the one that has testability enabled - and that would be debug in most cases:

enter image description here