qileilove

blog已经转移至github,大家请访问 http://qaseven.github.io/

IOS6.1单元测试持续集成实践

 最近项目测试需要,调研并实践了下IOS下单元测试工具和框架。目前比较流行的工具有xcode自带的OCUnit、GHUnit等,我选择的是GHUnit,因为相比OCUnit,GHUnit具有如下优势:
  1、开源框架
  2、支持重复测试、单一测试、集成测试。
  3、断言方法丰富
  4、支持持续集成
  5、测试类型多样(UI和Command Line)
  官方地址如下:http://gabriel.github.io/gh-unit/
  GitHub下载地址:https://github.com/gabriel/gh-unit
  下面我来讲下具体实现方法:
  一、测试准备
  1、xcode下载安装Command Line、测试的simulator
  2、若需要手机调试,按http://www.apkbus.com/android-465-1.html中方法设置
  3、下载GHUnit框架,并在Example/MyTestable-iOS中提取GHUnitiOS.framework
  二、新建项目
  1.首先,创建一个名为 GHUnitTest 的Empty Application,注意:不要选中 Include Unit Tests和 Use Core Data
  2.添加新的 test target,点击右侧的 Add Target,新增一个名为Test 的 Empty Application,让其附属于GHUnitTest,注意:不要选中 Include Unit Tests 和 Use Core Data
  3.向 Test添加 GHUnitIOS Framework,右击工程中的 Frameworks,选中 Add Files to...菜单,选取 GHUnitIOS.framework,targets 要选择Test
  4.设置 Test的 Build Settings:在 Other Linker Flags 中增加两个 flag: -ObjC 和 -all_load
  5.删除 Test下的 AppDelegate.h 和 AppDelegate.m 两个文件,注意不是GHUnitTest下
  6.选择编译目标 Test>iPhone 6.1Simulator,编译运行,应该能得到如下效果。目前我们还没有编写任何实际测试,所以列表为空。
  三、编写测试代码
  1.向 Test 工程中添加的 Objective C class测试类,继承于GHTestCase,并编写对应的测试方法,以下是一个AccessTest.m完整的测试类
#import "Test-Prefix.pch"
@interface AccessTest : GHTestCase
@end
@implementation AccessTest
- (void)setUpClass{
GHTestLog(@"Test Starts");
}
- (void)tearDownClass{
GHTestLog(@"Test ends");
}
-(void)tearDown{
[NSThread sleepForTimeInterval:1];
}
- (void)testOne{
GHTestLog(@"app starts first");
}
-(void)testTwo{    [
GHTestLog(@"app starts sencond");
}
@end


 2.修改main.m代码,以支持测试结果导出及持续集成
int main(int argc, char *argv[])
{
int retval;
@autoreleasepool {
if(getenv("GHUNIT_CLT")){
retval = [GHTestRunner run];
}else{
retval = UIApplicationMain(argc, argv, nil, @"GHUnitIOSAppDelegate");
}
}
return retval;
}
  四、编译运行
  1.编译运行后如图:
  2.点击run,每个case都会跑过去结束后可点击任意一个case,可显示成功的日志或者失败的内容,如图
五、持续集成
  方式一:命令行测试
  1.设置build periodically
  2.添加shell脚本
  GHUNIT_CLT=1 WRITE_JUNIT_XML=1 GHUNIT_AUTORUN=1 xcodebuild -target Test -sdk iphonesimulator6.1 -configuration Debug build
  3.添加Public JUnit test result report:GHUnitTest/build/test-results/*.xml,其他设置看需求,保存
  4.点击立即构建,控制会打出测试的内容,具体内容就不截图了,看最后的结果吧
  5.test-result如图
  方式二:UI测试
  与一的区别在于shell脚本如下:
instruments -t /Applications/Xcode.app/Contents/Applications/Instruments.app/Contents/PlugIns/AutomationInstrument.bundle/Contents/Resources/Automation.tracetemplate appName -e UIASCRIPT absolute_path_to_the_test_file

posted on 2013-12-12 10:39 顺其自然EVO 阅读(357) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 
<2013年12月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

导航

统计

常用链接

留言簿(55)

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜