jenkins钉钉接口通知自动化测试详情

Python代码

钉钉群机器人token获取请参考:jenkins构建结果钉钉群消息通知

#!\usr\bin\python3.6
# coding=utf-8
# Author: 文

from time import strftime
from requests import session

def nail_inform(statis):
    """
    :param statis: 测试用例统计 字典 key-sum: 总用例数 key-fail: 失败用例数  key-ratio: 测试通过率
    """

    # 钉钉接口与机器人token
    url = "https://oapi.dingtalk.com/robot/send?" \
          "access_token=34569456787221097652032705f0a2155b0794d98jb878641cbe8f2e7245"

    # 头部信息(Content-Type必填)
    header = {
        "Content-Type": "application/json",
        "Charset": "UTF-8"
    }
    
    now = strftime("%Y-%m-%d %H:%M:%S")    # 获取测试时间
    msg = "#自动化测试报告 #:  http://www.xxxx.com/job/{
    {jenkins中job名称}}/HTML_20Report/\n" \
          "时    间:  %s\n" \
          "总用例数:  %s\n" \
          "失败用例数:  %s\n" \
          "测试通过率:  %s\n\n" % (now, statis["sum"], statis["fail"], statis["ratio"])
    
    payload = {
        "msgtype": "text", 
        "text": {
            "content": msg      # 抓取数据发送的内容放到msg
        },
        "at": {
            "isAtAll": True,    # @全体成员(在此可设置@特定某人)
        }
    }

    r = session().post(url=url, json=payload, headers=header)
    print(r.json())             # 打印接口请求返回结果

if __name__ == "__main__":
    statis = {"sum": 29,"fail": 2,"ratio": "93.10%"}
    nail_inform(statis=statis)

注:源码如上,具体测试参数请从测试结果获取

自动化测试详情通知

发送情况如下:

jenkins钉钉接口通知自动化测试详情_第1张图片

你可能感兴趣的:(Jenkins,Requests接口自动化,selenium)