需求:公司需要将接口自动化部署到服务区上每天定时运行 并将测试报告发送到钉钉(飞书)相关群
框架逻辑
代码框架选择以requests+pytest+allure 技术为底层框架进行编写。
一、安装环境
- 1.1 安装Python
MAC安装和Windows安装这里就不多说了 下面提个安装包,自行下载 windows版本 链接: https://pan.baidu.com/s/1O96z1DzQ4DSqFXZHcGyWiQ 密码: 1h38 mac版本 链接: https://pan.baidu.com/s/1apJjhsUnrsUqi1Jxx8zBSA 密码: 488v
- 1.2 安装Pycharm
下面只提供安装包
windows版本 链接: https://pan.baidu.com/s/1Ijq7mpQTI4oUJtLNvxqi4w 密码: vg45 mac版本 https://www.macwk.com/soft/pycharm
- 1.3 安装插件
pip install requests pip install pytest pip install pytest-html allure安装:https://blog.csdn.net/chenfei_5201213/article/details/80982929
- 1.4 docker上安装Jenkins
docker run --name jenkins-blueocean -d -p 8080:8080 -p 50000:50000 -v jenkins-data:/var/jenkins_home jenkinsci/blueocean
输入 docker 查看jenkins镜像是否安装成功
docker images
查看容器是否运行
docker ps
启动jenkins
浏览器访问 http://<你的电脑ip>:8080 如果无法访问请关闭系统防火墙 密码存放在 cat /var/lib/jenkins/secrets/initialAdminPassword
- 1.5 编写飞书推送脚本
-
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2021/4/16 10:42 上午 # @Name : peilun # @File : Fly_book.py # @Software: PyCharm import sys sys.path.append("..") import jenkins from comment.readConfig import * from comment.log import * import requests import json ''' jenkins持续集成飞书发送报告通知 ''' class flyb(): def __init__(self): self.errorMsg = "" self.tile = time.strftime("%Y-%m-%d %H:%M:%S") try: self.jenkins_url = con.jenkins_url self.server = jenkins.Jenkins(self.jenkins_url, username=con.jenkins_name, password=con.jenkins_passwd) # 获取jenkins_url self.job_name = "job/autotest/" # job名称 self.job_url = self.jenkins_url + self.job_name # job的url地址 # 获取最后一次构建 self.job_last_number = self.server.get_info(self.job_name)['lastBuild']['number'] # #获取报告地址 self.report_url = self.job_url + str(self.job_last_number) + '/allure' # 报告地址 log.debug("jenkins地址:%s" % self.report_url) except Exception as e: self.errorMsg = str(e) log.info("jenkins连接异常 %s" %e) def ly_data(self): #获取生成的txt报告数据 d = {} proDir = "/allure-report/export/prometheusData.txt" f = open(con.allure_path + proDir, 'r') for lines in f: for c in lines: launch_name = lines.strip(' ').split(' ')[0] num = lines.strip(' ').split(' ')[1] d.update({launch_name: num}) f.close() retries_run = d.get('launch_retries_run') # 运行总数 status_passed = d.get('launch_status_passed') # 通过数量 status_failed = d.get('launch_status_failed') # 不通过数量 status_broken = d.get('launch_status_broken') # 异常数量 try: url_path = 'https://open.feishu.cn/open-apis/bot/v2/hook/' url = url_path+con.fly log.debug("webhook地址: %s"% url) headers = { 'content-type': "application/json" } payload = { "mobiles": "13282148187", "msg_type": "post", "content": { "post": { "zh_cn": { "title": "项目接口测试报告", "content": [ [ { "tag": "text", "text": "项目名称: " }, { "tag": "text", "text": "储能管家 " }, { "tag": "text", "un_escape": True, "text": "报告链接: " }, { "tag": "a", "text": "链接 ", "href": self.report_url }, { "tag": "text", "text": "监测分支: " }, { "tag": "text", "text": "master " }, { "tag": "text", "text": "运行总数: " }, { "tag": "text", "text": retries_run+" " }, { "tag": "text", "text": "通过数量: " }, { "tag": "text", "text": status_passed+" " }, { "tag": "text", "text": "异常数量: " }, { "tag": "text", "text": status_broken+" " }, { "tag": "text", "text": "不通过数量: " }, { "tag": "text", "text": status_failed+" " }, { "tag": "text", "text": "运行时间: " }, { "tag": "text", "text": self.tile } ] ] } } } } response = requests.request("POST", url, headers=headers, data=json.dumps(payload)) log.info(response.text) except Exception as e: self.errorMsg = str(e) log.info("接口请求异常 %s" % e) f = flyb() f.ly_data()
二、配置Jenkins
- 2.1 Jenkins内安装allues
-
2.2 创建项目
- 2.3 设置钉钉推送
三、遇到的问题
+关注 持续更新