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

IosSwiftUnit Testingswift5.4

Ios Problem Overview


I'm trying to use Swift's @testable declaration to expose my classes to the test target. However I'm getting this compiler error:

enter image description here

Intervals is the module that contains the classes I'm trying to expose. How do I get rid of this error?

Ios Solutions


Solution 1 - Ios

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 - Ios

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 - Ios

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 - Ios

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

Solution 5 - Ios

I started getting this error when running tests using Bitrise.

Unlike other users says, this is not per Target basis, or per Schema basis, it is per Configuration basis. Select Target -> Build Settings tab -> look for testability -> Enable it on the Configuration that you are using.

Please notice that Apple recommends to enable this on the configuration that you are using for debugging, not for AppStore.

Solution 6 - Ios

This is probably because your main target Enable Testability is set to NO. You should set it to YES in the debug scheme (which is used for running your tests).

If you use Carthage, this problem can be caused by importing frameworks with @testable, because they are built with a release scheme.

Most of the times it's bad practice to import frameworks with that prefix, so you could avoid it. If you can't, you should Enable Testability in the frameworks' release scheme. https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW326

Solution 7 - Ios

If by any chance you have

install! 'cocoapods',
         generate_multiple_pod_projects: true,
         incremental_installation: true

Then, this is the way to do it.

    # generated_projects only returns results if the we run "pod install --clean-install"
    # or install a pod for the first time

    installer.generated_projects.each do |project|
        project.build_configurations.each do |configuration|
            configuration.build_settings["ENABLE_TESTABILITY"] = "YES" 
        end
    end

Solution 8 - Ios

Above solution is fine if you are using pods/Carthage. But if you are using frameworks from iOS itself 'e.g. Contacts', you need add path to these frameworks in 'Library Search Paths' of your main project's target. enter image description here

Solution 9 - Ios

If you trying to test framework:

Go to test target -> Build Phase -> Create new copy files phase -> Choose frameworks -> Add all recursively used frameworks

Solution 10 - Ios

I'm a beginner and I just can't remember where all the settings and options are in Xcode. So I always feel frustrated when I see "you can set xxx to yyy ..." in answers to this kind of question. Where are those xxx in Xcode? and What are those yyy? We need a map showing how we get things done, but not another clue, like xxx. It seems we are Nicolas Cage in National Treasure!

Thanks for all the clues here, very much! You really help me. But I have a concrete answer here (which is actually from you):

  1. Select the main target of your project
  2. Go to the "Build Settings" tab
  3. Turn on the "All" option
  4. Search for the "Build Options" section
  5. Set "Enable Testability" to "Yes"
  6. Wait for a few minutes or restart Xcode (then the error disappeared)

Solution 11 - Ios

I got this when my new Tests.swift file belonged to the wrong Target.

Solution 12 - Ios

This didn't occur in my projects prior to Xcode 8, but after I upgraded to Xcode 8, it made me perplexed.

The answers posted here didn't get my problems resolved. For me, I just ditched these tests as it is not needed. So uncheck the test buttons:

enter image description here And now the error has gone out.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhgwhittleView Question on Stackoverflow
Solution 1 - IossgawView Answer on Stackoverflow
Solution 2 - IosTimView Answer on Stackoverflow
Solution 3 - IosBartłomiej SemańczykView Answer on Stackoverflow
Solution 4 - IosWladek SuralaView Answer on Stackoverflow
Solution 5 - IosrgkobashiView Answer on Stackoverflow
Solution 6 - IosSamuel B.View Answer on Stackoverflow
Solution 7 - IosNuno GonçalvesView Answer on Stackoverflow
Solution 8 - IosnikBhosaleView Answer on Stackoverflow
Solution 9 - IosZaporozhchenko OleksandrView Answer on Stackoverflow
Solution 10 - IosCable WView Answer on Stackoverflow
Solution 11 - IosGraham PerksView Answer on Stackoverflow
Solution 12 - IosBlaszardView Answer on Stackoverflow