iPhone/iPad Unit Test in xcode 4.3

   新建工程的时候看到一个Unit Test,就想干脆试试吧。说干就干,今天就新建了一个TestApp。其实可以直接勾选include Unit Test,不过我选择不勾选。

   点击进入工程可看到project和target两个大选项,直接点击Add Target,当然也可以在File菜单中选择New一个Target。选择ios的other里面的Cocoa Touch Unit Testing Bundle,新建一个TestAppTests,然后finish,一个新的target就创建到了Target项目里,在工程中会看到TestAppTests的文件夹,下面有同名的h和m文件,在m文件中新建的所有函数名以test开头的函数都会自动运行。

   1. 选中TestAppTests,首先选中Build Phases选项下的Target Dependencies,添加TestApp作为Target Dependency。

   2. 然后在它的build setting选项里面,查找bundle loader双击,并设置值为$(BUILT_PRODUCTS_DIR)/TestAppTests.app/TestAppTests

   3. 再搜索Test Host,并设置值为:$(BUNDLE_LOADER)。

   4. 之后点击应用程序的target,也就是你的工程TestApp的target,确保Symbols Hidden by Default为NO值。

   5. 这个时候看到的菜单product中的Test按钮是呈灰色状态, 如果我们希望在运行工程程序之前能进行测试,那么就需要Edit Scheme了,可以从菜单project进入,也可以点击工具栏中的Scheme,选择Edit Scheme,再选中需要设置的target:TestApp,点击左边的Test选项,看到右边下方的+-号了吗?点击+号,选择需要test的target,按下ok就行了。这步以后在菜单product中就能看到可选的Test按钮了,点击Test,会有Test Scceeded出现。

   6. 查看show the Log Navigator,点击TestAppTests,在右边的内容里面当鼠标移动到该条目的时候能看到变灰的条目右下方出现一个button,点击button就可以看到unit test的结果了。也可以在unit test文件中以打断点的方式查看。

   如果不做步骤2~4,在点击test的时候会出现以下错误:

Undefined symbols for architecture i386:
  "_OBJC_CLASS_$_TestAppTests", referenced from:
      objc-class-ref in TestAppTests.o


扫描二维码关注公众号,回复: 15430878 查看本文章




参考资料:

http://twobitlabs.com/2011/06/adding-ocunit-to-an-existing-ios-project-with-xcode-4/

猜你喜欢

转载自blog.csdn.net/rsp19801226/article/details/7353718