Objective-C 測試框架 GHUnit 的使用

fmms 12年前發布 | 55K 次閱讀 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中:

Objective-C 測試框架 GHUnit 的使用

選擇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欄):

Objective-C 測試框架 GHUnit 的使用

在Other Sources中新建一個預編譯頭文件tests_Prefix.pch,并在其中加入一 行:#import 。然后在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:

Objective-C 測試框架 GHUnit 的使用

點擊“Build and Run”,彈出模擬器窗口,點擊右上角的Run,測試運行結果如下:

Objective-C 測試框架 GHUnit 的使用

測試結果在界面和控制臺中都有顯示。

文章出處:http://blog.csdn.net/kmyhy/article/details/6780944

 本文由用戶 fmms 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!