Jenkins实现iOS自动化测试及覆盖率报告输出
2017-06-28 本文已影响686人
chsasaw
一、Jenkins安装,Xcode插件安装
略
二、Xcode工程准备
Xcode工程(这里我用的是workspace)包含两个Target,一个是UnitTest项目
![](https://img.haomeiwen.com/i1684547/f359ab0c35eac6b7.png)
打开Xcode左上角Manage Schemes,将Shared打钩
![](http://upload-images.jianshu.io/upload_images/1684547-56b327fbcac13585.png)
选中项目的Scheme点击左下角Edit,打开Gather coverage data,打开覆盖率收集,在Debug模式下会收集覆盖率报告。
![](http://upload-images.jianshu.io/upload_images/1684547-a49eef76657adc25.png)
写好UnitTestCase,command+u跑一下,在Xcode里可以看到用例结果报告和覆盖率报告。
![](http://upload-images.jianshu.io/upload_images/1684547-00ab0f07ecdbd37c.png)
![](http://upload-images.jianshu.io/upload_images/1684547-9f11fbc54b80d7a5.png)
三、集成到Jenkins实现自动化测试
新建Job,设置源码branch,这里最好新建一个专门用于测试的branch,这里取名unittest。
![](http://upload-images.jianshu.io/upload_images/1684547-bc854aa56a44fa55.png)
![](http://upload-images.jianshu.io/upload_images/1684547-7c0e51a09d029fbb.png)
设置构建触发器*/5 * * * *
,每5分钟检查一次源码变化。
![](http://upload-images.jianshu.io/upload_images/1684547-6ec541101a5aea16.png)
增加构建步骤,选择Execute shell脚本
![](http://upload-images.jianshu.io/upload_images/1684547-e61f2a654fc02913.png)
输入脚本:
#!/bin/bash -l
#新建目录用于保存报告
mkdir test-reports
#pod可能失败的全局参数设置
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
pod install
#xcodebuild test -workspace XXX.xcworkspace -scheme XXXTests -destination 'platform=iOS Simulator,name=iPhone 6s'跑测试用例
#-enableCodeCoverage YES 收集测试覆盖率
#ocunit2junit 输出报告转换为jenkins可读的junit报告
xcodebuild test -workspace XXX.xcworkspace -scheme XXXTests -destination 'platform=iOS Simulator,name=iPhone 6s' -configuration Debug -enableCodeCoverage YES 2>&1 | ocunit2junit
#slather coverage转换覆盖率报告为html文件,jenkins可读
#--input-format profdata xcode生成的为profdata格式的文件,转换为html以便jenkins显示
#--ignore 排除筛选需要计算的文件,多个格式写多个ignore表达式
slather coverage --html --input-format profdata --binary-basename XXXApp --scheme XXXTests --workspace XXX.xcworkspace --configuration Debug --ignore **View** --ignore **AppText** --output-directory reports XXX.xcodeproj
这里用到两个工具, ocunit2junit 以及slather.
尝试过用xctool,但是一直编译不成功,于是又换回了xcodebuild。
四、读取显示junit和覆盖率html报告
这里用到两个jenkins插件,jenkins->系统管理-> 管理插件,找到JUnit Plugin和HTML Publisher plugin,安装重启jenkins。
增加构建后操作,选择Publish Junit test result report
,配置xml文件路劲为第三步配置的test-reports/*.xml
。
![](http://upload-images.jianshu.io/upload_images/1684547-bd23cfb71300f77e.png)
再增加一个构建后操作,选择Publish HTML reports
, 配置html路劲为第三步配置的reports
,Index文件为index.html
,可以设置标题Reports title为Coverage Report
。
![](http://upload-images.jianshu.io/upload_images/1684547-39746e84c954e461.png)
点击立即构建,等待构建完成,返回job主页,可以看到junit测试结果报告和覆盖率的图表了。
![](http://upload-images.jianshu.io/upload_images/1684547-0698c7f9245dfd4e.png)
![](http://upload-images.jianshu.io/upload_images/1684547-408dc1f37c726932.png)
文章是后面写的,可能有遗漏的地方,如果有任何疑问和问题请给我留言。