1、GHUnit框架簡介
GHUnit是一個基於Object C的測試框架,支援Mac OSX 10.5和iOS 3.0以上版本。它具有以下特點:
支援在Xcode中運行測試、跟蹤中斷點、並直接與調試器交互;
從命令列運行或者使用MakeFile檔運行;
並行測試;
允許測試UI元件;
捕獲並顯示測試細節;
按照關鍵字查找測試案例;
查看測試日誌;
顯示堆疊和調試資訊;
在專案中以框架形式引用。
GHUnit是一個開源專案,專案位址:
HTTPs://github.com/gabriel/gh-unit#readme
這裡有框架的一個更詳細的介紹(英文),你可以在此下載框架的原始程式碼和二進位檔案。
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<GHUnitIOS/GHUnit.h>。然後在tests的Build設置中將Prefix Header設置為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,
測試結果在介面和主控台中都有顯示。
全站熱搜
留言列表