My Hieron Weblog

关于iPhone SDK Unit Test

by Anto on Apr.17, 2009, under iPhone Dev

由于这两天在写一个iPhone Midi的播放库,所以需要运行一些Unit Test,经过一番调研,发现现在有三个解决方案。他们是
1.  SDK2.2以后系统自带的OCUnit库,在XCode可以直接添加Unit Test Target和Cocoa Touch Test case,但对于在XCode里无缝的运行,我始终没找到任何答案,在现有2.2SDK和3.0SDK的文挡里,仍旧强调iPhone Unit Test仍未支持。不知道Apple到底为什么把没有完成的测试模块放到XCode里,还是没有时间更新文挡。总之最简便的路行不通。

2. 我用的方法实际于现有自带的OCUnit库,手工添加Unit Test的Target, 从而为我的项目添加Unit Tests. 在本文的最后我会具体描述一下配制方法。此种方法的优点是编译后马上能看到结果,但是缺点是无法做UI测试。

3. 另一个方法是用google-toolbox-for-mac中带的iPhone Unit Test 框架,其实此框架也是基于OCUnit库,但是区别是google-toolbox-for-mac添加了一部分自己的代码从而实现有限的UI测试。但是经过测试,我发现google-toolbox-for-mac的Unit Test 工具在>2.1编译的程序上无法正常工作。所以最终放弃了它。

配制步骤

1.  添加一个新的Target,例如UnitTests

从Project -> New Target, 选择添加一个Cocoa Touch->Unit Test Bundle.

2. 配制UnitTests Target

双击新添加的UnitTests Targets 打开Target 信息。在general页面里添加两个linked Library,一个是Fundation,一个是SenTestingKit(/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.sdk/Developer/Library/Frameworks/SenTestingKit.framework).

3.添加Target dependency

如果你希望你的项目每次编译的时候都自动运行Unit Test,你可以双击你的项目Target,在general页面上添加direct dependency为UnitTests。

4. 最后你可以添加的Test Case到UnitTests Target里,此代码编译后会提示test case 出错信息。


#import

@interface MyTests : SenTestCase {

}

@end


#include "TargetConditionals.h"
#if !TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR

#import “MyTests.h”

@implementation MyTests

- (void)testMyAddition {
STAssertEquals (1 + 1, 2, @”");
STAssertEquals (1 + 2, 2, @”");
}

@end
#endif

:

7 Trackbacks / Pingbacks for this entry

Leave a Reply

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...