首页 > 其他 > 详细

利用GHUnit编写iOS单元测试

时间:2014-03-03 16:30:55      阅读:561      评论:0      收藏:0      [点我收藏+]

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, @"校验2string是否相等");

}

@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运行,查看效果

bubuko.com,布布扣

单元测试代码见:https://github.com/tingxuan/UnitDemo


利用GHUnit编写iOS单元测试,布布扣,bubuko.com

利用GHUnit编写iOS单元测试

原文:http://blog.csdn.net/tingxuan_qhm/article/details/20304497

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!