XCode 下的 iOS 單元測(cè)試
CC 許可,轉(zhuǎn)載請(qǐng)注明出處
XCode 內(nèi)置了 OCUnit 單元測(cè)試框架,但目前最好用的測(cè)試框架應(yīng)該是 GHUnit。通過(guò) GHUnit + OCMock 組合,我們可以在 iOS 下進(jìn)行較強(qiáng)大的單元測(cè)試功能。本文將演示如何在 XCode 4.2 下使用 OCUnit, GHUnit 和 OCMock 進(jìn)行單元測(cè)試。
OCUnit在 XCode 下新建一個(gè) OCUnitProject 工程,選中 Include Unit Tests 選擇框:

OCUnit 框架則會(huì)為我們自動(dòng)添加 Unit Test 框架代碼:

XCode 在 OCUnitProjectTests.m 中為我們自動(dòng)生成了一個(gè) Fail 的測(cè)試:
@implementation OCUnitProjectTests
- (void)setUp
{
[super setUp];
// Set-up code here.
}
- (void)tearDown
{
// Tear-down code here.
[super tearDown];
}
- (void)testExample
{
STFail(@"Unit tests are not implemented yet in OCUnitProjectTests");
}
@end
讓我們來(lái)運(yùn)行 Test,看看效果:

從圖中的紅色下劃線部分可以看出,測(cè)試沒(méi)有通過(guò),符合預(yù)期。我們只要像類 OCUnitProjectTests 一樣編寫(xiě)繼承自 SenTestCase 類的子類,在其中添加形式如:- (void) testXXX(); 的測(cè)試函數(shù)既可,注意必須是一個(gè)無(wú)參無(wú)返回類型且名稱是以 test 為前綴的函數(shù)。
OCUnit 的有點(diǎn)是官方支持,與 XCode 集成的比較好。
GHUnit
GHUnit 是一個(gè)開(kāi)源的單元測(cè)試框架,具有可視化界面,功能亦相當(dāng)強(qiáng)大。Mark 寫(xiě)了一篇
OCUnit vs GHUnit 的文章,有興趣的童鞋可以看一看。
OCMock 是由 Mulle Kybernetik 為 OS X 和 iOS 平臺(tái)編寫(xiě)的遵循
mock object 理念的單元測(cè)試框架。
下面來(lái)介紹如何配置 GHUnit 和 OCMock
1,首先,創(chuàng)建一個(gè)名為 GHUnitProject 的單視圖應(yīng)用程序,注意:不要選中 Include Unit Tests 選擇框。然后運(yùn)行,應(yīng)該出現(xiàn)白屏。
2,添加新的 test target,選中左邊的工程名,點(diǎn)擊右側(cè)的 Add Target,新增一個(gè)名為 Tests 的 Empty Application 應(yīng)用程序,讓其附屬于 GHUnitProject注意:不要選中 Include Unit Tests 選擇框。


3,向 Tests 工程中(注意是 Tests 工程)添加 GHUnitIOS Framework。首先下載與 XCode 版本對(duì)應(yīng)的 GHUnitIOS Framework。英文好的可以直接查看官方 iOS 版的安裝文檔:點(diǎn)此查看,跳過(guò)此第 3 節(jié);否則請(qǐng)接著看。
3.1,解壓 GHUnitIOS 框架到 GHUnitProject 下,讓 GHUnitIOS.framework 與 Tests 在同一目錄下。
3.2,回到 XCode,右擊工程中的 Frameworks group,選中 Add Files to...菜單,選取 GHUnitIOS.framework ,注意 targets 要選擇 Tests。

3.3,設(shè)置 Tests 的 Build Settings:在 Other Linker Flags 中增加兩個(gè) flag: -ObjC 和 -all_load。

3.4,刪除 Tests 工程中的 UTSAppDelegate.h 和 UTSAppDelegate.m 兩個(gè)文件;3.5,修改 Tests 工程中的 main.m 為:
#import <UIKit/UIKit.h>
#import <GHUnitIOS/GHUnitIOSAppDelegate.h>
int main(int argc, char *argv[])
{
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([GHUnitIOSAppDelegate class]));
}
}
3.6,選擇編譯目標(biāo) Tests>iPhone 5.0 Simulator,編譯運(yùn)行,效果如下
。目前我們還沒(méi)有編寫(xiě)任何實(shí)際測(cè)試,所以列表為空。
4,編寫(xiě) GHUnit 測(cè)試。向 Tests 工程中添加名為 GHUnitSampleTest 的 Objective C class。其內(nèi)容如下:
GHUnitSampleTest.h
#import <GHUnitIOS/GHUnit.h>
@interface GHUnitSampleTest: GHTestCase
{
}
@end
GHUnitSampleTest.m
#import "GHUnitSampleTest.h"
@implementation GHUnitSampleTest
- (void)testStrings
{
NSString *string1 = @"a string";
GHTestLog(@"I can log to the GHUnit test console: %@", string1);
// Assert string1 is not NULL, with no custom error description
GHAssertNotNULL(string1, nil);
// Assert equal objects, add custom error description
NSString *string2 = @"a string";
GHAssertEqualObjects(string1, string2, @"A custom error message. string1 should be equal to: %@.", string2);
}
@end
然后編譯運(yùn)行,點(diǎn)擊 Run,效果如下:

圖中的 All 欄顯示所以的測(cè)試,F(xiàn)ailed 欄顯示沒(méi)有通過(guò)的測(cè)試。強(qiáng)大吧,GHUnit。你可以向 GHUnitSampleTest 添加新的測(cè)試,比如:
- (void)testSimpleFail
{
GHAssertTrue(NO, nil);
}
我們可以向 Tests 添加更多測(cè)試類,只要該類是繼承自 GHTestCase,且其中的測(cè)試方法都是無(wú)參無(wú)返回值且方法名字是以 test 為前綴即可。
OCMock