Objective-C 测试框架 GHUnit 的使用

fmms 12年前
     <p>2、在项目中安装GHUnit框架</p>    <p>新建Window-based Application项目,命名为GHUnitTest。</p>    <p>使用Add->New Target…添加一个target,使用模板 iPhone OS-> Cocoa Touch->Application,target 命名为tests(或者别的什么)。</p>    <p>把下载到的GHUnitIOS.framework文件拷贝到项目目录下。在frameworks中选择GHUnitIOS.framework,打开info窗口,切换到Targets面板,确保已正确地包含在了tests这个target中:</p>    <p><a href="https://simg.open-open.com/show/2efac094b2023b20121fc60b7b622fa3.png" target="_blank"><img style="cursor:pointer;" alt="Objective-C 测试框架 GHUnit 的使用" src="https://simg.open-open.com/show/2efac094b2023b20121fc60b7b622fa3.png" width="581" height="262" /></a></p>    <p>选择Targets下的tests,打开info窗口,在General页,通过左下角的+号按钮,把以下框架也包含进LinkedLibraries中:</p>    <p>CoreGraphics.framework</p>    <p>Foundation.framework</p>    <p>UIKit.framework</p>    <p>在Build面板,确保Framework Search Paths中已包含了GHUnitIOS.framework所在的路径;在OtherLinker Flags中加入-ObjC和-all_load。</p>    <p>将Tests-Info.plist文件中Main nib file base name一行删除。</p>    <p>将GHUnitIOSTestMain.m文件加入到项目中,下载地址:<a href="/misc/goto?guid=4959500529272874434" rel="nofollow">http://github.com/gabriel/gh-unit/blob/master/Project-iOS/GHUnitIOSTestMain.m</a>。</p>    <p>添加时,注意确保将文件包含到tests中(注意Add To Targets栏):</p>    <p><a href="https://simg.open-open.com/show/615619da1773bc38e86220d52359328e.png" target="_blank"><img style="cursor:pointer;" alt="Objective-C 测试框架 GHUnit 的使用" src="https://simg.open-open.com/show/615619da1773bc38e86220d52359328e.png" width="585" height="552" /></a></p>    <p>在Other Sources中新建一个预编译头文件tests_Prefix.pch,并在其中加入一 行:#import     <ghunitios ghunit.h="">      。然后在tests的Build设置中将Prefix Header设置为tests_Prefix.pch,这样就不需要每个测试类都import了。     </ghunitios></p>    <p>3、创建测试类</p>    <p>新建Objective C类MyTest。修改MyTest.h,将父类由NSObject修改为GHTestCase。修改MyTest.m,实现测试方法(方法名以test开头):</p>    <p>- (void)testStrings {       </p>    <p>    NSString *string1= @"a string";</p>    <p>   GHTestLog(@"I can log to the GHUnit test console: %@",string1);</p>    <p>    // Assert string1is not NULL, with no custom error description</p>    <p>   GHAssertNotNULL(string1, nil);</p>    <p>    // Assert equalobjects, add custom error description</p>    <p>    NSString *string2= @"a string";</p>    <p>    GHAssertEqualObjects(string1,string2, @"A custom error message. string1 should be equal to: %@.",string2);</p>    <p>}</p>    <p>把当前Build Configure设置为Simulator|Debug|tests:</p>    <a href="https://simg.open-open.com/show/d380c8a8ad0ce10ffa4204ed089e594a.png" target="_blank"><img style="cursor:pointer;" alt="Objective-C 测试框架 GHUnit 的使用" src="https://simg.open-open.com/show/d380c8a8ad0ce10ffa4204ed089e594a.png" width="385" height="373" /></a>    <p>点击“Build and Run”,弹出模拟器窗口,点击右上角的Run,测试运行结果如下:</p>    <p><a href="https://simg.open-open.com/show/ab582a35e16e03fff493437923e79bd3.png" target="_blank"><img style="width:673px;height:523px;cursor:pointer;" alt="Objective-C 测试框架 GHUnit 的使用" src="https://simg.open-open.com/show/ab582a35e16e03fff493437923e79bd3.png" /></a></p>    <p>测试结果在界面和控制台中都有显示。</p>    <p>文章出处:<a href="/misc/goto?guid=4959500529399263969" rel="nofollow">http://blog.csdn.net/kmyhy/article/details/6780944</a></p>