持续集成 微信小程序自动化测试——打通 Devops 流程

微信小程序云测服务 · 2022年06月30日 · 3441 次阅读

简介

小程序云测插件一般是用户手动在开发者工具提交任务。有些用户希望能够做到自动提交测试,将测试能力集成到 devops 流程中,做到定时或者自动触发。

现在云测插件新增第三方接口能力,支持直接用 https 接口调用的方式提测,方便用户将云测能力集成 devops 流程中。文档地址:
https://developers.weixin.qq.com/miniprogram/dev/devtools/minitest/api_exe.html

本文以 Jenkins 为例,详细介绍云测如何和 Jenkins 打通

环境准备

注:环境准备以 Window10+Python 项目 +Git 代码版本管理 +Jenkins 为例介绍,可根据需求准备环境,例如 Linux 操作系统,SVN 代码版本管理,Java 项目等
以下步骤非必须,如果已安装可以跳过

  • 安装 JDK 8
  • 安装 Python 38
  • 安装 Git
  • 下载 Jenkins (https://www.jenkins.io/download/
  • 安装 Jenkins(可查看用户手册 https://www.jenkins.io/doc/book/getting-started/
  • 创建管理员用户
  • 配置 Git(这里以 Git 存储测试脚本举例,用户也可以用 SVN 等其他工具存储)
    • 安装 git 插件 Manage Jenkins > Manage Plugins 安装 git plugins
    • 配置 git 工具 Manage Jenkins > Global Tool Configuration
  • 添加凭据 Manage Jenkins > Manage credentials > 添加凭据

Jenkins 实现小程序云测自动测试

  • 新建 Item
    例如选择 Freestyle project

  • 配置

1. 选择 GitHub 项目

2.选择 Git 并添加凭据,在 Additional Behaviours 中选择 Check out to a sub-directory

3.构建触发器,选择 Build periodically

4.构建,选择 Execute Windows batch command,并输入需要执行的命令

其中 test.py 脚本为请求云测第三方服务接口代码

class MiniTestApi:
    def __init__(self, user_token, group_en_id):
         self.token = user_token          # 需要填写自己的token
         self.group_en_id = group_en_id   # 项目的英文ID
         self.minitest_api = 'https://minitest.weixin.qq.com/thirdapi/plan'

   def third_auto_task(self):
         """
         提交测试任务
         :return:
         """
         config = {
             "assert_capture": True,
             "auto_relaunch": True,
             "auto_authorize": False,
             "audits": True,
             "compile_mode": "pages/getOpenId/index?p=1&m=2"
         }

         data = {
             'token': self.token,
             'group_en_id': self.group_en_id,
             'test_type': 2,
             'platforms': 'android,ios',
             'wx_id': '',
             'wx_version': 3,
             'desc': 'Minium测试',
             'test_plan_id': 6,
             'dev_account_no': 1,
             'minium_config': config,
         }
         resp = requests.post(
             self.minitest_api,
             json=data
         )
         resp = resp.json()
         print(resp)
         return resp
   def query_auto_task(self, plan_id):
         """
         查询测试任务
         :param plan_id:
         :return:
         """
         data = {
             'token': self.token,
             'group_en_id': self.group_en_id,
             'plan_id': plan_id,
         }
         resp = requests.get(
             self.minitest_api,
             params=data
         )
         resp = resp.json()
         print(resp)

if __name__ == '__main__':
     minitest_client = MiniTestApi('xxx', 'xxx')
     result = minitest_client.third_auto_task()
     if "plan_id" not in result["data"].keys():
         exit(1)
     plan_id = result["data"]["plan_id"]
     time.sleep(10)
     minitest_client.query_auto_task(plan_id)   
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册