Xcode 4 单元测试链接器错误
注意:“使用 GHUnit”不是此问题的可接受答案。我知道大多数人认为 GHUnit 比 Xcode4 OCUnit 更好,但这不是我要问的。我将单独评估。
我有一个在 Xcode4 中从头开始创建的 Xcode 项目,在创建过程中选中了“包含单元测试”复选框。我还包含了一些我在之前的项目中开发的库。它们通过“将文件添加到 x...”对话框添加到项目中,并且仅添加到应用程序目标(而不是测试目标)。它们在运行应用程序时工作正常,所以我认为它们设置正确。我还为这个项目编写了许多不同的课程。
我的测试文件是以标准方式设置的,名为 [AppName]Tests.h 和 .m。
标题代码:
<代码>#import < SenTestingKit/SenTestingKit.h >
@interface [AppName]Tests : SenTestCase {
@private
}
@end
实现代码:#import "[AppName]Tests.h"
@implementation [AppName]Tests
- (void)setUp
{
[super setUp];
// Set-up code here.
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
// Test methods go here
@end
这只是基本框架。它在我的其他项目中运行良好,并且在这个项目中只要我不导入任何其他文件即可。当我从该项目导入另一个文件并使用它时,我在 Xcode 输出日志中看到以下错误:无法加载位于 /Users/[Me]/Library/Developer/Xcode/DerivedData/[AppName]-dwuuuwcpmdqxqmgxomoniplwhlpb/Build/Products/Debug-iphonesimulator/[AppName]Tests.octest 的测试包,因为链接错误发生。 dyld 可能无法找到测试包所链接的框架或库,可能是因为该框架或库在链接时的安装路径不正确。
我已经验证了:
- 所有框架我用过 添加到“链接二进制文件 应用程序和测试的库” 目标。
- 测试目标已配置 正确构建,我所有的 测试方法显示在编辑中 方案...->测试->测试
- 除了这一个问题之外的每一个问题都已经过 已解决,并且没有编译器 错误。
- 讨论的所有设置 此处 设置正确且相同 到我的另一个测试项目 正确。
有什么想法可能导致这种情况吗?
NOTE: "Use GHUnit" is not an acceptable answer to this question. I know most think GHUnit is better than the Xcode4 OCUnit, but that's not what I'm asking about. I'll evaluate that separately.
I have an Xcode project that I created in Xcode4 from scratch, with the "Include unit tests" checkbox checked during creation. I have also included some libraries I developed in a previous project. They were added to the project via the "Add Files to x..." dialog and only added to the application target (not the testing target). They work fine when running the application, so I think they're set up correctly. I also have a number of various classes written for this project.
My testing files are set up in the standard way, named [AppName]Tests.h and .m.
Code for header:#import < SenTestingKit/SenTestingKit.h >
@interface [AppName]Tests : SenTestCase {
@private
}
@end
Code for implementation:#import "[AppName]Tests.h"
@implementation [AppName]Tests
- (void)setUp
{
[super setUp];
// Set-up code here.
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
// Test methods go here
@end
Which is just the basic skeleton. It works fine in my other project, and in this project as long as I don't import any other files. When I do import another file from this project and use it, I see the following error in the Xcode output log:The test bundle at /Users/[Me]/Library/Developer/Xcode/DerivedData/[AppName]-dwuuuwcpmdqxqmgxomoniplwhlpb/Build/Products/Debug-iphonesimulator/[AppName]Tests.octest could not be loaded because a link error occurred. It is likely that dyld cannot locate a framework framework or library that the the test bundle was linked against, possibly because the framework or library had an incorrect install path at link time.
I've already verified that:
- All the frameworks I use have been
added to "Link Binary with
Libraries" for both the app and test
targets. - The test target has been configured
to build correctly and that all my
test methods show up in Edit
Scheme...->Test->Tests - Every issue but this one has been
resolved and there are no compiler
errors. - All the settings discussed
here
are set up correctly and identical
to my other project that tests
correctly.
Any thoughts on what might be causing this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
我必须将单元测试目标上的“测试主机”属性设置为
$(BUNDLE_LOADER)
。这解决了我的问题!I had to set the "Test Host" property on the unit test target to
$(BUNDLE_LOADER)
. That solved my problem!我只是在这个和类似的错误上浪费了几个小时 - 结果我已经重命名了我的主要目标 - 尝试通过重命名相关变量和删除整个 DerivedData 目录来解决这个问题是不成功的。
我最终只是按照此处的步骤设置了一个新的单元测试目标:
http://twobitlabs. com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/
现在一切都很好。
因此,如果您遇到奇怪的、无法解释的链接错误,您最好创建一个新的单元测试目标。只需要2分钟。
I just wasted hours on this and similar errors - turns out I had renamed my main target - attempts to fix this by renaming the relevant variables and by removing the entire DerivedData directory were unsuccessful.
I eventually simply set up a new unit test target following the steps here:
http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/
And everything is fine now.
So - if you have strange, inexplicable link errors you might be better off just creating a new unit test target. Only takes 2 minutes.
我做了以下事情:
http://twobitlabs. com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/
深入了解 Test_Hosts = $(BUNDLE_LOADER)
设置Test_Hosts 修复了完全相同的问题!
I did the following:
http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/
Take a deeper look at Test_Hosts = $(BUNDLE_LOADER)
Setting the Test_Hosts fixed exactly the same issue!
当我在运行单元测试时遇到链接器错误时,有两件事可以解决我的问题。第一个解决方案是将“构建后测试”构建设置设置为“是”并选择“产品”>“构建”。构建>构建测试以运行测试。该解决方案是更容易实施的解决方案。
第二种解决方案是将应用程序的实现文件添加到单元测试目标中。通过选择“查看”>“打开文件检查器”公用事业>文件检查员。在项目导航器中选择一个实施文件。在文件检查器中选中单元测试目标旁边的复选框。
对于在模拟器中运行的 iPhone 应用程序,请确保测试主机构建设置为空。模拟器不支持应用程序托管的单元测试。
When I get linker errors running unit tests, two things fix the problem for me. The first solution is to set the Test After Build build setting to YES and choose Product > Build For > Build For Testing to run the tests. This solution is the easier one to implement.
The second solution is to add the application's implementation files to the unit test target. Open the file inspector by choosing View > Utilities > File Inspector. Select an implementation file in the project navigator. Select the checkbox next to the unit test target in the file inspector.
For iPhone applications running in the simulator, make sure the Test Host build setting is blank. The simulator does not support application-hosted unit tests.
我曾经遇到过同样的问题。由于某种原因,我的项目的源文件之一也包含在内以供测试目标编译,这会导致此链接错误。
通过确保仅编译测试实现文件,您应该能够解决此错误。您可以在以下位置检查:
TestTarget ->构建阶段 ->编译源
I had this same problem once. For some reason one of the source files for my project was also included to be compiled for the test target, which causes this link error.
By making sure that only the test-implementation files are compiled, you should be able to resolve this error. You can check this at:
TestTarget -> Build Phases -> Compile Sources
运行 Xcode 4.5.2 时遇到此错误 - 这是 2012 年 11 月 - 以上均不起作用。似乎设置捆绑加载器和测试主机应该为您填充项目中的所有依赖项 - 或者在应用程序的环境或其他环境中运行测试,但不幸的是对我来说不起作用。它所做的就是阻止 Xcode 发出有关缺少哪些文件/库的特定警告。
对我有用的是添加一个新目标:cocoa touch 单元测试(确保捆绑加载程序和测试主机构建设置为空),观察构建错误并手动添加缺少的依赖项 - 逐一添加来自的所有源文件我需要的项目,然后是框架。不是很优雅,但我很高兴让它发挥作用。不知道为什么我还没有尝试过这个 GHUnit 库。
Came across this error running Xcode 4.5.2 - it's Nov 2012 - none of the above worked. It seems that setting the bundle loader and test host is supposed to fill in all the dependencies from your project for you - or run the tests in your app's environment or something, but unfortunately wasn't working for me. What it did do was prevent the specific Xcode warnings about which files/libraries were missing.
What worked for me was adding a new target: cocoa touch unit test, (making sure the bundle loader and test host build settings were empty), watching the build errors and manually adding in the missing dependencies - one by one all the source files from my project that were needed and then the Frameworks. Not very elegant but I was just happy to get it working. Not sure why I haven't tried this GHUnit library yet.
确保测试目标将应用程序目标配置为依赖项(构建阶段 -> 目标依赖项)。
Make sure that the test target has the app target configured as a dependency (Build Phases -> Target Dependencies).
我知道这个问题已经很老了,但我只是在同一问题上挣扎了一段时间并最终设法解决了它,所以让我分享一下我的发现。
我一直在将一个应用程序从 iOS 移植到 Mac,该项目本身是为 iOS 创建的,因此项目和主要目标在支持的平台上都有 iOS。现在,当我开始移植时,我为 Mac 创建了新目标,并将仅针对该目标的支持平台更改为 OSX。之后我创建了另一个目标进行单元测试,但忘记将支持的平台从 iOS 更改为 Mac。我想你已经应该知道问题是什么了,基本上单元测试目标默认与 Cocoa 框架链接,因此由于该目标支持的平台只有 iOS,因此 cocoa 框架从未构建且未正确链接。将测试目标的支持平台更改为 OSX 解决了该问题。
我知道这对于 iOS 测试目标可能不是很有帮助,但至少转到您的测试目标 Link Binary With Libraries 部分并查看是否有任何红色库。这给了我一个想法,也许它也会对你们中的一些人有所帮助。
I'm aware that this question is quite old, but I just struggled with identical issue for some time and finally managed to solve it, so let me share what I found.
I've been porting an app from iOS to Mac, the project itself was created for iOS so both the project and main target had iOS in supported platforms. Now, when I started porting I created new target for Mac, and changed supported platforms to OSX only for that target. After that I created another target for unit tests, but forgot to change the supported platforms from iOS to Mac. I think you already should know what the problem was, basically the unit tests target is by default linked with Cocoa framework so since the supported platforms for this target was only iOS the cocoa framework was never built and wasn't properly linked. Changing supported platforms to OSX for the test target fixed the problem.
I'm aware that this may not be very helpful for iOS test targets, but at least go to your test target Link Binary With Libraries section and see if there are any red libraries. This gave me the idea, maybe it will also help some of you.
我最近在几次合并后遇到了这个问题,我和一位同事都将文件添加到了项目中,并且并非所有实现文件都是单元测试目标的成员。
解决方案:
.m
文件(使用“导航器中的过滤器”(cmd-opt-j) 搜索.m
)注意:如果测试目标复选框显示
-
而不是+
或未选中,则意味着某些(但不是全部)选定文件是目标的成员。我尝试过在合并时使用
.xcodeproj
的project.pbxproj
文件做更智能的事情,但每次我都因为它的难以理解而放弃并求助于这种方法。(另一个注意事项:我一次只选择几个文件,原因有两个:
I've run into this after several merges lately where a coworker and I have both added files to the project, and not all of the implementation files are a member of the unit testing target.
The solution:
.m
files (use "Filter in Navigator" (cmd-opt-j) to search for.m
)Note: if the test target checkbox shows
-
instead of+
or being unchecked, it means that some, but not all, of the selected files are a member of the target.I've tried doing more intelligent things at merge time with the
.xcodeproj
'sproject.pbxproj
file, but I've given up out of frustration with its inscrutability every time and fallen back on this method.(Another note: I only select a few files at a time for two reasons: