一. 配置KIF相关
-
如果创建项目时没有选择test,则需要单独创建
Unit Testing Bundle
注意:KIF必须使用Unit Testing Bundle
否则会报错 -
pod添加KIF,并进行拉取
target 'UITestAPP' do
target 'UITestAPPTests' do
pod 'KIF', :configurations => ['Debug']
end
end
3. 创建测试文件
- 添加accessibilityLabel
- 书写简单测试代码
#import <KIF/KIF.h>
@interface LoginTest : KIFTestCase
@end
@implementation LoginTest
// MARK: 每个测试用例前准备
- (void)beforeEach {
}
// MARK: 每个测试用例后恢复状态
- (void)afterEach {
if ([tester tryFindingViewWithAccessibilityLabel:@"登录页->退出" error:nil]) {
[tester tapViewWithAccessibilityLabel:@"登录页->退出"];
}
}
// MARK: 测试用例 账号为空 密码为空 正常账号密码
- (void)testLogin {
if (![tester tryFindingViewWithAccessibilityLabel:@"登录页->账号" error:nil]) {
return;
}
// 账号为空
[tester clearTextFromViewWithAccessibilityLabel:@"登录页->账号"];
[tester clearTextFromAndThenEnterText:@"123456" intoViewWithAccessibilityLabel:@"登录页->密码"];
[tester tapViewWithAccessibilityLabel:@"登录页->登录"];
// 密码为空
[tester clearTextFromAndThenEnterText:@"admin" intoViewWithAccessibilityLabel:@"登录页->账号"];
[tester clearTextFromViewWithAccessibilityLabel:@"登录页->密码"];
[tester tapViewWithAccessibilityLabel:@"登录页->登录"];
// 正常
[tester clearTextFromAndThenEnterText:@"admin" intoViewWithAccessibilityLabel:@"登录页->账号"];
[tester clearTextFromAndThenEnterText:@"123456" intoViewWithAccessibilityLabel:@"登录页->密码"];
[tester tapViewWithAccessibilityLabel:@"登录页->登录"];
}
@end
-
点击测试完毕
-
添加使能统计覆盖率
-
添加测试scheme,否则jenkins会报错
二. 配置Ruby环境
- 下载
ocunit2junit
和slather
$ sudo gem install ocunit2junit
$ sudo gem install slather
- 如果安装
slather
失败,可能是系统ruby版本太低原因,更新ruby
-
- 安装rvm管理ruby工具
curl -L get.rvm.io | bash -s stable
-
- 如果报错
Failed to connect to raw.githubusercontent.com:443
输入命令
- 如果报错
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:789
-
- 再次安装
curl -L get.rvm.io | bash -s stable
-
- 安装后改变路径
source ~/.rvm/scripts/rvm
-
- 获取最新的ruby版本
rvm list known
-
- 下载最新的ruby版本
rvm install 3.0.0
-
- 切换Ruby版本
rvm use system // 使用系统的ruby
rvm use 2.4.0 // 使用安装的ruby
rvm use 2.4.0 --default // 设置默认版本
-
- 也可以删除某个版本
rvm remove 2.4.0
-
- 切换到最新版本Ruby即可
rvm use 3.0.0
三. 配置Jenkins环境
曾尝试使用docker进行管理,其中路径什么的很难都隐藏在容器中,无法直观看到,最终直接安装Jenkins.
- 安装jenkins
brew install jenkins
- 安装完毕终端输入jenkins,打开1080端口等待启动完毕,终端会给一个密码
具体步骤参考: macOS Jenkins安装&配置
四. 代码由码云管理,配置码云相关
- 将代码上传的码云。
- 下载插件gitee
- 添加码云私人令牌与访问账号密码
码云上获取私人令牌
同理添加码云的账号密码,用于访问库
五. 创建并配置Item
- 创建Item
2. 配置Item
1. 添加描述与连接gitee相关
2. 配置代码拉取
3. 添加运行命令
3. 输入测试命令
# 判断文件夹是否存在,不存在则创建
if [ ! -d "test-reports" ]; then
mkdir mkdir test-reports
else
echo "test-reports 已经存在"
fi
#pod可能失败的全局参数设置
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8
export LC_ALL=en_US.UTF-8
# 加载pod
pod install
# 配置测试模拟器等
xcodebuild test -workspace UITestAPP.xcworkspace -scheme UITestAPPTests -destination 'platform=iOS Simulator,name=iPhone 11' -configuration Debug -enableCodeCoverage YES 2>&1 | ocunit2junit
# 覆盖率检查并以html显示
slather coverage --html --input-format profdata --binary-basename UITestAPP --scheme UITestAPPTests --workspace UITestAPP.xcworkspace --configuration Debug --ignore **View** --ignore **AppText** --output-directory reports UITestAPP.xcodeproj
- 运行jenkins
- 查看jenkins文件结构
6. 配置构建后显示
全部配置完毕,主要难点为 ruby版本切换,从新配置pod非常浪费时间,覆盖率复选框有时候会找不到,要加Test Plans