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

前端 未结 10 826
难免孤独
难免孤独 2020-12-04 14:00

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

相关标签:
10条回答
  • 2020-12-04 14:29

    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.

    0 讨论(0)
  • 2020-12-04 14:32

    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.

    0 讨论(0)
  • 2020-12-04 14:33

    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

    0 讨论(0)
  • 2020-12-04 14:34

    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

    0 讨论(0)
提交回复
热议问题