Jenkins持续集成体系建设
2021-07-01 本文已影响0人
InsaneLoafer
目录
- 任务目标
- 安卓App构建、打包、部署与自动化测试
- 安卓App持续集成体系建设实战
一、任务目标
- 实战任务的故事
- 从源码制作出可运行App,支持测试工作
- 各种自动任务
- Daily Build打包
- Daily Build发布
- Daily Build UI自动测试(BVT - Build Verification Test)
- 利用技术来帮助做 “杂活儿”
- 优势:
- 重复性定式劳动任务,
- 减少人力参与,提升工作效率
- Build不再是阻碍测试进度的因素
- 自动化重用率高,维护频率低
二、 安卓App构建、打包、部署与自动化测试
涉及技术点
- Android App构建
- Android SDK使用
- 安卓模拟器或真机
- Python + Appium自动化测试
- Jenkins持续集成
- Shell脚本开发
- Java程序开发
- Git ( github使用)
测试App构建、打包过程
- Android应用(例子程序)
- 源码位置:https://github.com/princeqjzh/AndroidSampleApp
- 安卓打包的目标文件:app-debug.apk文件
- 安卓打包命令:
gradlew clean assembleDebug
- 安卓安装包的输出路径:
/app/build/outputs/apk/debug/app-debug.apk
- 关于安卓打包的环境要求
- JDK 1.8
- Android SDK
-
Gradle
image.png
image.png
安卓App部署
-
前提条件:
- 安卓实体机或者安卓模拟器
- 安卓SDK
-
目标:通过命令安装/卸载App
-
命令控制apk安装与卸载
- 安装:
adb install <file_ path>
- 卸载:
adb uninstall <package_ name>
- 安装:
-
查找包名:
adb shell pm list packages | grep sample
image.png
安卓UI自动化测试
- Appium app自动测试
- 源码位置:https://github.com/princeqjzh/iAppBVT_Python
- 开发工具:PyCharm
- UI自动化框架:Appium
- 测试执行框架:pytest
- 实现验证点:
- App可安装
- App可启动
- App首页预期的元素存在
- 校验步骤
- 启动App
- 检查Record Event是否存在
-
结束输出结果
image.png
测试源码
-
目录树
image.png
-
Readme.me
**iApp BVT自动化测试程序(Python版)**
运行环境:
- appium server
- python3
- unittest, pytest
- git
配置文件:iAppBVT_Python.json
- 将配置文件复制到本地磁盘
- 填入设备的 deviceName 与 udid
运行命令:
pytest -sv test/bvt_test.py --tc-file /full path/iAppBVT_Python.json --tc-format json
- bvt_test.py 文件
from appium import webdriver
import unittest
import time
from pytest_testconfig import config
timeout = 30 # 超时
poll = 2 # 轮询
class IAppBVT(unittest.TestCase):
def setUp(self):
desired_caps = {}
appium_server_url = config['appium_server_url']
desired_caps['platformName'] = config['desired_caps']['platformName']
desired_caps['udid'] = config['desired_caps']['udid']
desired_caps['deviceName'] = config['desired_caps']['deviceName']
desired_caps['appPackage'] = config['desired_caps']['appPackage']
desired_caps['appActivity'] = config['desired_caps']['appActivity']
desired_caps['automationName'] = config['desired_caps']['automationName']
desired_caps['noReset'] = config['desired_caps']['noReset']
self.driver = webdriver.Remote(appium_server_url, desired_caps)
def tearDown(self):
self.driver.quit()
def test_bvt(self):
print('BVT test is started!')
status = self.is_record_event_btn_exist()
print(f'The record event button is exist - {status}')
self.assertTrue(status, f'Check record_button result is {status}')
print('Test finished!')
def is_record_event_btn_exist(self):
elem = self._find_elem_by_xpath('//android.widget.Button[contains(@resource-id,"id/trackEventButton")]')
return elem is not None
def _find_elem_by_xpath(self, elem_xpath, time_out=timeout, raise_exception=True):
start = time.time()
elem = None
while time.time() - start < time_out and elem is None:
time.sleep(poll)
try:
elem = self.driver.find_element_by_xpath(elem_xpath)
except Exception:
print('by pass the element not found')
if elem is None and raise_exception:
raise LookupError(f'The element which xpath is {elem_xpath} could not be found')
return elem
- iAppBVT_Pyhon.json 文件
- 获取udid:
adb devices
image.png
{
"desired_caps": {
"platformName": "Android",
"deviceName": "<your device name>",
"udid": "<your device udid>",
"noReset": "false",
"automationName": "UiAutomator2",
"appPackage": "com.appsflyer.androidsampleapp",
"appActivity": ".MainActivity"
},
"appium_server_url": "http://localhost:4723/wd/hub"
}
- requirements.txt
allure-pytest
appium-python-client
pytest
pytest-testconfig
requests
selenium
urllib3
三、安卓App持续集成体系建设实战演练
- 将之前内容所做事情用Jenkins任务来实现
- Jenkins平台源码构建、打包、发布app测试包
- Jenkins自动部署、测试新app测试包
- Jenkins通知运行结果
实现流程图

安卓打包任务

安卓APP发布 输出apk文件

安卓app自动部署、测试任务

App打包任务与自动部署、测试任务关联
-
实现完成打包任务之后,运行自动测试任务,安装新包,并运行UI自动测试验证新安装包的正确性
image.png
配置任务结果邮件通知
- 任务完成之后要把结果周知到相关人员,可以通过email的方式进行通知

-
结果通知:任务正确完成
image.png
三、安卓App持续集成体系建设实战
创建打包任务
-
创建job
image.png
-
选择任务运行的节点-master
image.png
-
添加项目的GitHub地址,并复制项目名称
image.png
-
将复制的项目名称复制到sub-directory
image.png
-
添加shell命令
. ~/.bash_profile
cd AndroidSampleApp
sh gradlew clean assembleDebug

-
添加发布路径:
AndroidSampleApp/app/build/outputs/apk/debug/app-debug.apk
image.png
-
进行构建,查看控制台输出以及运行结果
image.png
image.png
创建自动化测试任务
-
创建任务
image.png
-
选择打包的机器
image.png
-
添加项目的GitHub地址,复制项目名称
image.png
-
粘贴项目名称到sub-directory
image.png
-
输入shell命令
- 如果apk 不存在执行
uninstall
命令会报错,可以用以下命令过渡,中间的| |
是或者的意思,先之前前面的命令,如果前面的命令报错则执行后面的命令
{
adb uninstall com.appsflyer.androidsampleapp
} | | {
echo 'no app package installed on the device'
}
-
pwd
命令是为了获取当前路径
. ~/.bash_profilepwd= `pwd`
apk=$pwd/../1219_buildAndroidsample/AndroidSampleApp/app/build/outputs/apk/debug/app-debug.apk
{
adb uninstall com.appsflyer.androidsampleapp
}||{
echo 'no app package installed on the device'
}
adb install $apk
cd iAppBVT_Python
pip3.9 install -r requirements.txt
pytest -sv test/bvt_test.py --tc-file /Users/jizhiqian/iAppBvT_Python.json --tc-format json

-
构建job,进行运行
image.png
配置消息通知
-
打包job和自动化测试job添加 Email Notification
image.png
关联任务
-
在打包job中添加关联,Post-bulid Actions -> Build other projects
image.png
构建打包任务会自动触发自动化测试脚本


四、课程总结
- 从源码到可测包,外加自动化测试验证的全流程持续集成体系建设
- 安卓App的构建、打包方法
- Apk文件发布
- 触发自动测试
- Appium UI自动化测试
- 利用Jenkins平台进行持续集成演练
五、FAQ
如何做失败后的自动重试
- 在job的 Post-build Actions 中添加 Retry build after failure 插件
- Fixed -> Fixed delay:表示延迟多少秒再次构建
- Maximum number of successive failed builds:重试次数
- 可以捕捉错误信息,使用正则表达式,
|
表示或
