KIF的全称是Keep it functional。它是一个建立在XCTest的UI测试框架,通过accessibility来定位具体的控件,再利用私有的API来操作UI。由于是建立在XCTest上的,所以你可以完美的借助XCode的测试相关工具。
1,命令行安装pod:
sudo gem install cocoapods
2,修改或创建工程的pod文件 :Podfile,如下所示:
# platform :ios, '9.0'
target 'TenMinDemo' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for CYXText
pod 'AFNetworking', '~> 3.0'
pod "SDWebImage"
pod "MJExtension"
pod "MJRefresh"
pod 'SVProgressHUD'
pod 'ReactiveObjC'
end
target 'TenMinDemoTests' do
use_frameworks!
pod 'KIF', '~> 3.5.1'
end
其中如下一段内容为新添加的:
target 'TenMinDemoTests' do //测试工程名
use_frameworks!
pod 'KIF', '~> 3.5.1'
end
3,执行安装命令:
pod install
安装相应的内容。
4,在现有工程中添加Target实现
选择File→New→Target…菜单项, 从中选择iOS→Other中的Cocoa Touch Unit Testing Bundle模板.如下图所示:
单击下一步,进行相应的设置页:
5,设置测试工程相关项:
(1) Product Name:KIF测试工程名,可以自由命名,最好是测试工程名+”Tests”。
(2) Organization Name, Organization identifier, Bundle identifier,根据需要自行全名即可。
(3) Language:编码语言,有Objective-C和Swift,默认选择OC.
(4) Project和Target to be Tested:为对应的要测试的工程名,一定要保证是正确的。
(5) 单击“Finish”创建完成。
(6) 工程创建完成,如下所示:
// TenMinDemoTestsB
//
// Created by GrowingIO on 2018/5/30.
// Copyright © 2018年 SXF. All rights reserved.
//
#import <XCTest/XCTest.h>
@interface TenMinDemoTests : XCTestCase
@end
@implementation TenMinDemoTests
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}
@end
各个函数相互作用及执行顺序如下图所示:
(2)输入用户名:dingdone和密码:123456,单击“登录”按钮,登录成功。
(3)检测是否进入到了列表页。
(1)KIF API的中文翻译https://www.jianshu.com/p/87bbbb798926,
感觉好像不太全;
(2)官网上的源码及其介绍:https://github.com/kif-framework/KIF.
(3) KIFUITestActor Doc:http://cocoadocs.org/docsets/KIF/3.2.1/Classes/KIFUITestActor.html,这个网站内容比较全面,只是英文的,不翻墙能不能打开还不清楚。
很多新的技术都有一个特点,就是相关的文档非常少,这也没有办法;可是换个想法来看,如果相关技术文档到处都是,这项技术学了也没有什么优势了。
目前KIF一个Kiftestcase类就相当于一个测试用例集,我们可以简单地利用类的方式来组织测试用例,类中的每个以test****开头的函数均是一个测试用例。当然基于KIF开源的特性,我们可以开发其他的用例集管理方式或是工具,如下图所示:
(图片来源于:https://tech.meituan.com/iOS-UITest-KIF.html)
任何自动化测试最终的归宿都是CI(持续化集成),当然我们的KIF自动化也需要做持续化集成。而在做持续化集成前,需要调研如何通过命令行来执行测试用例?
(1)将项目设置成shared
从product->Scheme->manage schemes,查看项目是否是shared,如果不是,则选中后面的复选框将其共享。
(2)借助于xctool来执行测试用例
Xctool源码地址:https://github.com/facebook/xctool,可以去查看一下如何安装和使用:https://blog.csdn.net/jeikerxiao/article/details/51669451
而运行我们的示例代码应该是:
xctool -workspace XXX.xcworkspace –schem XXYYY run-tests XXXtests:测试用例集 -sdk "iphonesimulator11.3"
同时可以通过-only来指定运行某个用例
(a)运行单个测试用例
xctool -workspace TenMinDemo.xcworkspace -scheme TenMinDemo run-tests -only TenMinDemoTests:LoginTest/testlogin -sdk "iphonesimulator11.3"
(b)运行一个测试用例集
xctool -workspace TenMinDemo.xcworkspace -scheme TenMinDemo run-tests TenMinDemoTests:LoginTest -sdk "iphonesimulator11.3"
1,生成junit报告:
xctool -workspace TenMinDemo.xcworkspace -scheme TenMinDemo run-tests TenMinDemoTests:LoginTest -sdk "iphonesimulator11.3" -reporter junit:/Users/growingio/Documents/report.xml
2,转换测试报告xml成html
由于xctool输出的报告中含有中间输出信息,所以不能直接转换,需要先处理一下,去掉无用的信息。或是先将报告转化成json格式,然后再提取出合适的信息,生成xml文件。这个方法需要另外写脚本转换一下。
生成报告的步骤如下:
(1)安装ant
(2)优化xctool生成的xml,把中间输出信息去掉
(3)创建builld.xml文件
在做持续化集成的时候,一定要考虑到因为环境原因,网络原因等造成的非正常失败的情况,故要加上用例失败重跑机制。
Commands如下所示:
由于经验有限,难免会存在不足之处,后续会不断更新和完善。
一, 测试环境搭建
KIF框架依赖工程源码进行测试的,所以要能从开发处拿到被测试工程的源码,然后在搭建相应的测试环境。1,命令行安装pod:
sudo gem install cocoapods
2,修改或创建工程的pod文件 :Podfile,如下所示:
# platform :ios, '9.0'
target 'TenMinDemo' do
# Uncomment the next line if you're using Swift or would like to use dynamic frameworks
# use_frameworks!
# Pods for CYXText
pod 'AFNetworking', '~> 3.0'
pod "SDWebImage"
pod "MJExtension"
pod "MJRefresh"
pod 'SVProgressHUD'
pod 'ReactiveObjC'
end
target 'TenMinDemoTests' do
use_frameworks!
pod 'KIF', '~> 3.5.1'
end
其中如下一段内容为新添加的:
target 'TenMinDemoTests' do //测试工程名
use_frameworks!
pod 'KIF', '~> 3.5.1'
end
3,执行安装命令:
pod install
安装相应的内容。
4,在现有工程中添加Target实现
选择File→New→Target…菜单项, 从中选择iOS→Other中的Cocoa Touch Unit Testing Bundle模板.如下图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/56c0b89a8699391dbe96f291d49d0c20.png)
单击下一步,进行相应的设置页:
5,设置测试工程相关项:
![](https://i-blog.csdnimg.cn/blog_migrate/d6b8ad602119f43ee4b41c783eb98171.png)
(1) Product Name:KIF测试工程名,可以自由命名,最好是测试工程名+”Tests”。
(2) Organization Name, Organization identifier, Bundle identifier,根据需要自行全名即可。
(3) Language:编码语言,有Objective-C和Swift,默认选择OC.
(4) Project和Target to be Tested:为对应的要测试的工程名,一定要保证是正确的。
(5) 单击“Finish”创建完成。
(6) 工程创建完成,如下所示:
![](https://i-blog.csdnimg.cn/blog_migrate/99ac63d0f314db94668bd3dc8c2d5bf0.png)
- 生成TenMinDemoTests工程,同时生成TenMinDemoTests.m文件和info.plist文件。
- 在Products文件夹中生成“TenMinDemoTests.xctest”文件。
- TenMinDemoTests.m文件内容如下:
// TenMinDemoTestsB
//
// Created by GrowingIO on 2018/5/30.
// Copyright © 2018年 SXF. All rights reserved.
//
#import <XCTest/XCTest.h>
@interface TenMinDemoTests : XCTestCase
@end
@implementation TenMinDemoTests
- (void)setUp {
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testExample {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct results.
}
- (void)testPerformanceExample {
// This is an example of a performance test case.
[self measureBlock:^{
// Put the code you want to measure the time of here.
}];
}
@end
各个函数相互作用及执行顺序如下图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/c1e2a704e1eae590ddc12bc415918b3a.png)
二, 测试用例编写
KIF和其他测试框架类似,通过OC编写代码实现我们的测试步骤,进而去检测操作完成的结果。下面我们以一个简单而完整的用例来说明一下测试用例如何编写:1,手工用例步骤介绍:
(1)我从网上下载的一个Demo,工程名为TenMinDemo,选择其中的一个功能,登录:打开app,选择“博文”项(2)输入用户名:dingdone和密码:123456,单击“登录”按钮,登录成功。
(3)检测是否进入到了列表页。
2,代码测试用例如下:
//LoginTests.m上面的代码添加了详细的注释,内容也比较简单,就不逐步介绍了。注意:KIF是基于XCText的,基本的判断语句都是XCTest的。
//
// LoginTest.m
// TenMinDemoTests
//
// Created by GrowingIO on 2018/5/31.
// Copyright © 2018年 SXF. All rights reserved.
//
#import "LoginTest.h"
@implementation LoginTest
- (void)setUp {
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
- (void)testLogin {
//单击“博文”
[tester tapViewWithAccessibilityLabel:@"博文"];
//输出用户名和密码
[tester clearTextFromViewWithAccessibilityLabel:@"usename"];
[tester enterText:@"dingdone\n" intoViewWithAccessibilityLabel:@"usename"];
[tester clearTextFromViewWithAccessibilityLabel:@"password"];
[tester enterText:@"123456\n" intoViewWithAccessibilityLabel:@"password"];
//等待1秒
[tester waitForTimeInterval:1];
//单击登录按钮
[tester tapViewWithAccessibilityLabel:@"login"];
[tester waitForTimeInterval:5];
//获取列表页view
UITableView *utable=[tester waitForViewWithAccessibilityLabel:@"tableView"];
//获取tableview第二行
NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
UITableViewCell *utvc=[tester waitForCellAtIndexPath:index inTableView:utable];
if (@available(iOS 11.0, *)) {
//判断第二行的内容是否符合预期
//NSLog(@"Cell Content:%@",utvc.textLabel.text);
XCTAssertEqualObjects(utvc.textLabel.text, @"第2行");
} else {
// Fallback on earlier versions
}
}
@end
3,KIF API简介
KIF API相关的介绍挺多,只是不太全面,下面我介绍几个不错的网站:(1)KIF API的中文翻译https://www.jianshu.com/p/87bbbb798926,
感觉好像不太全;
(2)官网上的源码及其介绍:https://github.com/kif-framework/KIF.
(3) KIFUITestActor Doc:http://cocoadocs.org/docsets/KIF/3.2.1/Classes/KIFUITestActor.html,这个网站内容比较全面,只是英文的,不翻墙能不能打开还不清楚。
很多新的技术都有一个特点,就是相关的文档非常少,这也没有办法;可是换个想法来看,如果相关技术文档到处都是,这项技术学了也没有什么优势了。
三,测试用例集及命令行执行
目前KIF一个Kiftestcase类就相当于一个测试用例集,我们可以简单地利用类的方式来组织测试用例,类中的每个以test****开头的函数均是一个测试用例。当然基于KIF开源的特性,我们可以开发其他的用例集管理方式或是工具,如下图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/a66e73426b5812d76021f8b69972d00a.png)
(图片来源于:https://tech.meituan.com/iOS-UITest-KIF.html)
任何自动化测试最终的归宿都是CI(持续化集成),当然我们的KIF自动化也需要做持续化集成。而在做持续化集成前,需要调研如何通过命令行来执行测试用例?
(1)将项目设置成shared
从product->Scheme->manage schemes,查看项目是否是shared,如果不是,则选中后面的复选框将其共享。
![](https://i-blog.csdnimg.cn/blog_migrate/520930a7b6220336ea1f81e887fb3caa.png)
(2)借助于xctool来执行测试用例
Xctool源码地址:https://github.com/facebook/xctool,可以去查看一下如何安装和使用:https://blog.csdn.net/jeikerxiao/article/details/51669451
而运行我们的示例代码应该是:
xctool -workspace XXX.xcworkspace –schem XXYYY run-tests XXXtests:测试用例集 -sdk "iphonesimulator11.3"
同时可以通过-only来指定运行某个用例
(a)运行单个测试用例
xctool -workspace TenMinDemo.xcworkspace -scheme TenMinDemo run-tests -only TenMinDemoTests:LoginTest/testlogin -sdk "iphonesimulator11.3"
(b)运行一个测试用例集
xctool -workspace TenMinDemo.xcworkspace -scheme TenMinDemo run-tests TenMinDemoTests:LoginTest -sdk "iphonesimulator11.3"
四,生成测试报告
xctool的reporter选项可以生成不同形式的测试报告:- pretty: (默认) 一个文字化的输出器,使用 ANSI 颜色和 unicode 符号来进行美化输出。
- plain: 类似 pretty, 不过没有颜色和 Unicode。
- phabricator: 把构建/测试的结果输出为 JSON 数组,它可以被 Phabricator 的代码评审工具读取。
- junit: 把测试结果输出成和 JUnit/xUnit 兼容的 XML 文件。
- json-stream: 一个由构建/测试事件组成的 JSON 字典流,每行一个(示例输出)。
- json-compilation-database: 输出构建事件的 JSON Compilation Database ,它可以用于基于 Clang Tooling 的工具,例如 OCLint.
1,生成junit报告:
xctool -workspace TenMinDemo.xcworkspace -scheme TenMinDemo run-tests TenMinDemoTests:LoginTest -sdk "iphonesimulator11.3" -reporter junit:/Users/growingio/Documents/report.xml
2,转换测试报告xml成html
由于xctool输出的报告中含有中间输出信息,所以不能直接转换,需要先处理一下,去掉无用的信息。或是先将报告转化成json格式,然后再提取出合适的信息,生成xml文件。这个方法需要另外写脚本转换一下。
生成报告的步骤如下:
(1)安装ant
(2)优化xctool生成的xml,把中间输出信息去掉
(3)创建builld.xml文件
<project name="TestNG_WORKSPACE" default="junit-report" basedir=".">(4)将juntreport.xml的路径设置好,同时将build.xml给保存到相应的位置,然后在build.xml的路径下执行命令:ant,则会生成如下格式的html报告
<!-- Sets the property variables to point to respective directories -->
<property name="junit-xml-dir" value="${basedir}/junitreports"/> //junit报告文件的路径
<property name="report-dir" value="${basedir}/html-report" /> //生成html生成报告的位置
<!-- Ant target to generate html report -->
<target name="junit-report">
<!-- Delete and recreate the html report directories -->
<delete dir="${report-dir}" failοnerrοr="false"/>
<mkdir dir="${report-dir}" />
<mkdir dir="${report-dir}/Junit" />
<!-- Ant task to generate the html report.
todir - Directory to generate the output reports
fileset - Directory to look for the junit xml reports.
report - defines the type of format to be generated.
Here we are using "noframes" which generates a single html report.
-->
<junitreport todir="${report-dir}/Junit">
<fileset dir="${junit-xml-dir}">
<include name="**/*.xml" />
</fileset>
<report format="noframes" todir="${report-dir}/Junit" /> //format:"frames",还左边框的报告,“noframes”,无边框的报告
</junitreport>
</target>
</project>
![](https://i-blog.csdnimg.cn/blog_migrate/9b070b8bdc8e9366a9358cf713baff64.png)
五,持续化集成
持续化集成的核心思想就是借助于Jenkins的任务调度功能,将自动化测试用例放到Git/Svn上,然后配置相应的代码库地址,执行脚本命令等。根据不同的触发条件,完成自动运行的机制。其中还要根据需要进行相关的配置等等,网上相应的步骤比较多,在此就不详细介绍了,可以参考一下:“基于 KIF 的 iOS UI 自动化测试和持续集成(https://tech.meituan.com/iOS-UITest-KIF.html)”在做持续化集成的时候,一定要考虑到因为环境原因,网络原因等造成的非正常失败的情况,故要加上用例失败重跑机制。
Commands如下所示:
xctool -workspace TenMinDemo.xcworkspace -scheme TenMinDemo run-tests TenMinDemoTests:LoginTest -sdk "iphonesimulator11.3" -reporter junit:/Users/growingio/Documents/report.xml
array=( TimerTests HistoryTests )
for data in ${array[@]}
do
xctool -workspace TenMinDemo.xcworkspace -scheme TenMinDemo run-tests -only TenMinDemoTests:${data} -sdk "iphonesimulator11.3" -reporter junit:/Users/growingio/Documents/report.xml
done
六,总结
最近在公司以KIF框架做了一些IOS自动化测试相关的东西,中间磕磕碰碰的遇到了不少问题,也查了资料进行了解答。现在就整个项目的实现过程给总结一下,以方便知识的总结和记录,也有利于后来者进行快速入门。由于经验有限,难免会存在不足之处,后续会不断更新和完善。