您的位置:首页 > 移动开发 > IOS开发

利用GHUnit编写iOS单元测试

2014-03-02 20:44 309 查看
GHUnit是一个开源的objective-c的unit test框架,他比起OCUnit来说,配置简单一些,没有Logic Tests和Application Tests的分别,但使用起来需要第三方库GHUnitIOS.framework的支持。GHUnit有GUI的界面,并非仅仅能看到log文件,比较直观,编写test
case和OCUnit没有太多的区别。

言规正传,下面就来看如何使用。

1、创建UnitDemo的workspace文件,再创建UnitDemo的project,在项目文件根目录创建Podfile文件,通过Podfile将两者关联起来

platform :ios,'6.0'
inhibit_all_warnings!
workspace'UnitDemo'
xcodeproj'UnitDemo.xcodeproj'

2、创建Target:GHUnitTestDemo,注意选择(Empty Application)
3、添加GHUnit的framework依赖,通过cocoapods管理,通过修改Podfile文件,添加如下行

#add GHUnit Framework
link_with'GHUnitTestDemo'
pod'GHUnitIOS','~> 0.5.8'

4、下载依赖并更新

1) pod install --no-repo-update
2)pod update

5、更改Target:GHUnitTestDemo的build settings

在 Other Linker Flags 中增加两个 flag: -ObjC 和 -all_load。

6、编写测试类

TXLogicTest.h文件
#import "GHTestCase.h"

@interface TXLogicTest : GHTestCase

@end

TXLogicTest.m文件

#import "TXLogicTest.h"

@implementation TXLogicTest
-(void)testStringEquals2Another
{
    NSString * stringA =@"stringA";
    NSString * stringB =@"stringB";
    GHAssertEqualObjects(stringA, stringB,
@"校验2个string是否相等");
}
@end

7、修改main.m文件

#import <UIKit/UIKit.h>
#import "GHUnitIOS/GHUnitIOSAppDelegate.h"
int main(int argc,
char * argv[])
{
    @autoreleasepool {
        returnUIApplicationMain(argc, argv,nil,NSStringFromClass([GHUnitIOSAppDelegateclass]));
    }
}

8、command+R运行,查看效果



单元测试代码见:https://github.com/tingxuan/UnitDemo
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  GHUnit 单元测试 iOS