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
'Module was not compiled for testing' when using @testable
提问by hgwhittle
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:
我正在尝试使用 Swift 2 的新@testable
声明将我的类公开给测试目标。但是我收到这个编译器错误:
Intervals
is 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 Testability
build 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 cocoapods
as a dependency manager
就我而言,我使用自定义构建配置进行测试(称为Test
),也cocoapods
用作依赖项管理器
I had to add the following lines to the end of my Podfile
to 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 cocoapods
sets ENABLE_TESTABILITY
to YES
only for Debug
builds
默认情况下cocoapods
设置ENABLE_TESTABILITY
为YES
仅用于Debug
构建
回答by Bart?omiej Semańczyk
回答by Wladek Surala
回答by Samuel B.
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).
这可能是因为您的主要目标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 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
大多数情况下,导入带有该前缀的框架是不好的做法,因此您可以避免它。如果你不能,你应该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 Target
basis, or per Schema
basis, it isper Configuration
basis.
Select Target
-> Build Settings
tab -> 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.
如果您使用的是 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:
此处发布的答案没有解决我的问题。对我来说,我只是放弃了这些测试,因为它不需要。所以取消选中测试按钮: