Objective-C 測試框架 GHUnit 的使用
2、在項目中安裝GHUnit框架
新建Window-based Application項目,命名為GHUnitTest。
使用Add->New Target…添加一個target,使用模板 iPhone OS-> Cocoa Touch->Application,target 命名為tests(或者別的什么)。
把下載到的GHUnitIOS.framework文件拷貝到項目目錄下。在frameworks中選擇GHUnitIOS.framework,打開info窗口,切換到Targets面板,確保已正確地包含在了tests這個target中:
選擇Targets下的tests,打開info窗口,在General頁,通過左下角的+號按鈕,把以下框架也包含進LinkedLibraries中:
CoreGraphics.framework
Foundation.framework
UIKit.framework
在Build面板,確保Framework Search Paths中已包含了GHUnitIOS.framework所在的路徑;在OtherLinker Flags中加入-ObjC和-all_load。
將Tests-Info.plist文件中Main nib file base name一行刪除。
將GHUnitIOSTestMain.m文件加入到項目中,下載地址:http://github.com/gabriel/gh-unit/blob/master/Project-iOS/GHUnitIOSTestMain.m。
添加時,注意確保將文件包含到tests中(注意Add To Targets欄):
在Other Sources中新建一個預編譯頭文件tests_Prefix.pch,并在其中加入一 行:#import
3、創建測試類
新建Objective C類MyTest。修改MyTest.h,將父類由NSObject修改為GHTestCase。修改MyTest.m,實現測試方法(方法名以test開頭):
- (void)testStrings {
NSString *string1= @"a string";
GHTestLog(@"I can log to the GHUnit test console: %@",string1);
// Assert string1is not NULL, with no custom error description
GHAssertNotNULL(string1, nil);
// Assert equalobjects, add custom error description
NSString *string2= @"a string";
GHAssertEqualObjects(string1,string2, @"A custom error message. string1 should be equal to: %@.",string2);
}
把當前Build Configure設置為Simulator|Debug|tests:

點擊“Build and Run”,彈出模擬器窗口,點擊右上角的Run,測試運行結果如下:
測試結果在界面和控制臺中都有顯示。