简体   繁体   English

是否可以使用XCode的SenTestingKit对静态库项目进行单元测试?

[英]Is it possible to unit test a static library project using XCode's SenTestingKit?

I've created an iOS unit test target for doing logic tests following the steps provided in Apple's documentation. 我按照Apple文档中提供的步骤创建了一个iOS单元测试目标,用于进行逻辑测试。

However my build fails and i get the following error: 但是我的构建失败,我收到以下错误:

Undefined symbols: 未定义的符号:
"_OBJC_CLASS_$_MyClass", referenced from: objc-class-ref-to-MyClass in LogicTests.o ld: symbol(s) not found collect2: ld returned 1 exit status “_OBJC_CLASS _ $ _ MyClass”,引自:LogicTests.o中的objc-class-ref-to-MyClass ld:未找到符号collect2:ld返回1退出状态

Ordinarily, if I wanted to use my static library within an application I would include the library.a file, and the headers(including the MyClass.h file...). 通常,如果我想在应用程序中使用我的静态库,我会包含library.a文件和头文件(包括MyClass.h文件......)。 Is something additional required to run logic tests on a static library WITHIN that same project if my test cases are utilizing MyClass.h ? 如果我的测试用例使用MyClass.h,那么在同一项目的静态库上运行逻辑测试需要额外的东西吗?

Tjhanks Tjhanks

Due to the nature of static libraries, you can't perform application tests , which by the sound of it is what you are trying to do. 由于静态库的性质,您无法执行应用程序测试 ,因为它是您正在尝试执行的操作。 However, you can perform logic tests. 但是,您可以执行逻辑测试。

You were correct in your observation about unit testing in the client application. 您对客户端应用程序中的单元测试的观察是正确的。

The Xcode template optionally includes unit tests, but if you go to the build settings for that unit test you will see it doesn't specify a test host or bundle loader. Xcode模板可选地包括单元测试,但是如果您转到该单元测试的构建设置,您将看到它没有指定测试主机或捆绑加载器。 This is because of the nature of static libraries. 这是因为静态库的本质。 They are not applications, they are libraries - so you can do logic tests , you cannot do application tests . 它们不是应用程序,它们是库 - 所以你可以做逻辑测试你不能做应用程序测试

Application tests you may wish to perform on your static library may include the following scenario: 您可能希望在静态库上执行的应用程序测试可能包括以下方案:

My library creates an SQLite database at runtime, I wish to perform a unit test to check everything is inserting and/or updating as expected. 我的库在运行时创建一个SQLite数据库,我希望执行单元测试来检查所有内容是否按预期插入和/或更新。

In order to test this with unit tests, one must create another application which includes or otherwise is dependant of your library. 为了使用单元测试对此进行测试,必须创建另一个包含依赖于您的库的应用程序。 This application then includes your library and application tests may then be set up there. 然后,此应用程序包含您的库,然后可以在那里设置应用程序测试。

Apple有一个示例(UnitTests),展示了如何执行此操作: https//developer.apple.com/library/ios/#samplecode/UnitTests/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011742

I actually just solved it. 我其实刚解决了。 I had to copy all of the .m files in my project to the LogicTest target's 'Compile Sources'. 我不得不将项目中的所有.m文件复制到LogicTest目标的'Compile Sources'。 As well as add the frameworks the sources reference to the 'Link Binary With Libraries' section of the target. 除了添加框架之外,源代码还引用了目标的“Link Binary With Libraries”部分。

I hope this helps others 我希望这有助于其他人

是的,Xcode 4.2有一个带有测试的Cocoa Touch静态库模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM