ios 使用@testable 时“模块未编译用于测试”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30787674/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 06:22:16  来源:igfitidea点击:

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

iosswiftunit-testingswift2

提问by hgwhittle

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

我正在尝试使用 Swift 2 的新@testable声明将我的类公开给测试目标。但是我收到这个编译器错误:

enter image description here

在此处输入图片说明

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

Intervals是包含我试图公开的类的模块。我如何摆脱这个错误?

回答by sgaw

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

在您的主要目标中,您需要将Enable Testability构建选项设置为是。

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

根据下面@earnshavian 的评论,这应该仅用于按照苹果发行说明的调试版本:“启用可测试性构建设置应仅在您的调试配置中使用,因为它禁止依赖于不导出内部符号的优化应用程序或框架” https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW326

回答by Tim

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

就我而言,我使用自定义构建配置进行测试(称为Test),也cocoapods用作依赖项管理器

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

我必须将以下几行添加到我的末尾Podfile以启用可测试性

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 cocoapodssets ENABLE_TESTABILITYto YESonly for Debugbuilds

默认情况下cocoapods设置ENABLE_TESTABILITYYES仅用于Debug构建

回答by Bart?omiej Semańczyk

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

确保您在应用方案下正确设置了复选框。您应该取消选中Archive Build 的测试目标。

enter image description here

在此处输入图片说明

回答by Wladek Surala

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:

对于那些仅在运行 Xcode Profiler 时遇到这种情况的人:将方案管理中的配置文件构建配置切换到启用可测试性的配置 - 在大多数情况下这将是调试:

enter image description here

在此处输入图片说明

回答by Samuel B.

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

这可能是因为您的主要目标Enable Testability设置为NO. 您应该YES在调试方案(用于运行测试)中将其设置为。

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

如果您使用 Carthage,则此问题可能是由使用 导入框架引起的@testable,因为它们是使用发布方案构建的。

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 Testabilityin the frameworks' release scheme. https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW326

大多数情况下,导入带有该前缀的框架是不好的做法,因此您可以避免它。如果你不能,你应该Enable Testability在框架的发布方案中。https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Chapters/Introduction.html#//apple_ref/doc/uid/TP40001051-CH1-SW326

回答by rgkobashi

I started getting this error when running tests using Bitrise.

使用 Bitrise 运行测试时,我开始收到此错误。

Unlike other users says, this is notper Targetbasis, or per Schemabasis, it isper Configurationbasis. Select Target-> Build Settingstab -> look for testability-> Enable it on the Configuration that you are using.

不像其他的用户说,这是不是Target基础,或每Schema基础,它Configuration基础。选择Target->Build Settings选项卡 -> 查找testability-> 在您使用配置上启用它。

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

请注意,Apple 建议在您用于调试的配置上启用此功能,而不是在 AppStore 上启用。

回答by Nuno Gon?alves

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

回答by nikBhosale

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

如果您使用的是 pods/Carthage,则上述解决方案很好。但是,如果您使用的是 iOS 本身的框架“例如联系人”,则需要在主项目目标的“库搜索路径”中添加这些框架的路径。 在此处输入图片说明

回答by Zaporozhchenko Oleksandr

If you trying to test framework:

如果您尝试测试框架:

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

转到测试目标 -> 构建阶段 -> 创建新的副本文件阶段 -> 选择框架 -> 添加所有递归使用的框架

回答by Blaszard

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

这在我在 Xcode 8 之前的项目中没有出现过,但是在我升级到 Xcode 8 之后,它让我感到困惑。

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 hereAnd now the error has gone out.

在此处输入图片说明现在错误已经消失了。