使用静态库进行单元测试 [英] Unit Testing in With A Static Library

查看:211
本文介绍了使用静态库进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有用户界面项目(UI)和核心逻辑项目(Core)的XCode工作区。我想在UI项目中进行OCUnit单元测试,所以我已经添加了一个新的测试目标,就像通常那样。

I have an XCode workspace with a user interface project (UI) and a core logic project (Core). I want OCUnit unit tests in the UI project, so I have added a new target for testing, as is commonly done.

我可以运行测试,直到我为主UI目标中的类放入import语句,后者又引用Core项目。

I am able to run tests just fine until I put in import statements for classes in the main UI target which in turn reference the Core project.

我得到的错误是词法或预处理器问题'xxx.h'文件未找到。当我直接构建主UI目标时,我没有收到此消息。

The error I get is "Lexical or Preprocessor Issue 'xxx.h' file not found". I do not get this message when I build the main UI target directly.

这就好像主UI目标在构建时知道Core,但是当它被引用时从测试目标来看,似乎对Core一无所知。

It's as if the main UI target knows about Core when it is built, but when it is referenced from the test target it seems to know nothing about Core.

我采取了使用链接二进制文件库添加对核心项目的引用的步骤清单仍然是红色的。线索?也许吧,但是Link列表中的红色引用并没有阻止UI目标构建和使用核心类。我还使主要目标成为测试目标的依赖。

I took the step of adding a reference to the core project using the "Link Binaries with Libraries" The item in the list remains red. A clue? Maybe, but the red reference in the Link list doesn't keep the UI target from building and using core classes. I also made the main target a dependency of the test target.

推荐答案

请务必查看Apple示例代码单元测试:
https://developer.apple.com/library/ios/#samplecode/UnitTests/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011742

Make sure you check out the Apple sample code "Unit Tests": https://developer.apple.com/library/ios/#samplecode/UnitTests/Introduction/Intro.html#//apple_ref/doc/uid/DTS40011742

确保您的库项目在OCUnit测试目标的构建阶段设置为Dependancy,并且它作为库链接。

Make sure your library project is set as a Dependancy in your OCUnit test target's build phases, AND it's linked as a library.

打开您的项目Xcode中。在文件菜单中,转到项目设置...(或工作区设置...如果您使用工作区)。单击高级...并确保选中唯一。清理并重建。

Open up your project in Xcode. In the File menu, go to Project Settings... (or Workspace Settings... if you are using a workspace). Click Advanced... and make sure Unique is checked. Clean and rebuild.

检查您的BUILD_PRODUCTS_DIR以查看您图书馆的标题是否显示在那里。如果他们不这样做,首先检查您的库目标中的构建阶段,以确保您需要的标题位于公共部分(项目部分可能也可以,但尝试公开,看看是否能解决您的问题)。

Check your BUILD_PRODUCTS_DIR to see if the headers for your library show up in there. If they don't, first check the build phases in your library target to make sure the headers you need are in the Public section (the Project section may work as well, but try Public and see if that solves your issue).

这涵盖了人们在您的情况下遇到的最常见问题。如有疑问,请检查UnitTests示例中的目标设置。祝你好运!

That covers the most common problems people seem to run into in your situation. When in doubt, check the target settings in the UnitTests sample against yours. Good luck!

这篇关于使用静态库进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆