一,新建钉钉机器人
1.钉钉群右上角点击群设置,选择智能群助手,点击添加机器人,选择自定义机器人;
在这里插入图片描述
2.给机器人起个名字,消息推送开启,复制出webhook,后面会用到,勾选自定义关键词,填写关键词(关键词可以随便填写,但是一定要记住,后面会用);
在这里插入图片描述
在这里插入图片描述
二,钉钉机器人发送消息
url就是创建机器人时的webhook,data中的atMobiles可填写多个手机号,发送的消息会直接@这个人,text的content里面一定要加上创建机器人时设置的关键词,msgtype意思时文本格式,也可以link格式,就可以放链接了;

    def send_text(self):

        url = "https://oapi.dingtalk.com/robot/send?access_token=43c4dab2ac31125e605c458b4b9561a73"
        headers = {'Content-Type': 'application/json'}
        data = {"at": {"atMobiles":["18206264857"],"atUserIds":["user123"],"isAtAll": False},
                "text": {"content":"砍价小程序接口自动化测试"},"msgtype":"text"},"msgtype":"text"}
        requests.post(url,headers=headers,data=json.dumps(data))

三,钉钉机器人实际的应用
1.监控接口自动化结果
在这里插入图片描述
实现思路是:jenkins定时执行自动化——执行完后生成html报告——BeautifulSoup模块解析html报告——发送钉钉消息
如下代码:
解析html的模块:

from common.handle_path import html_path
from bs4 import BeautifulSoup

class GetHtml:
    """
    读取测试报告,解析html  获得测试用例总数,通过数等,发送到钉钉
    """

    def get_total(self):
        with open(html_path, "r", encoding="utf-8") as f:
            file = f.read()
            soup = BeautifulSoup(file, 'html.parser')  # 使用BeautifulSoup库解析网页内容
            item = soup.find_all("p")[1].string  # 使用BeautifulSoup库的标签方法找到你需要的内容
            return str(item)

    def get_pass(self):
        with open(html_path, "r", encoding="utf-8") as f:
            file = f.read()
            soup = BeautifulSoup(file, 'html.parser')  # 使用BeautifulSoup库解析网页内容
            item = soup.find_all("span",class_="passed")[0].string  # 使用BeautifulSoup库的标签方法找到你需要的内容
            return str(item)

    def get_skipped(self):
        with open(html_path, "r", encoding="utf-8") as f:
            file = f.read()
            soup = BeautifulSoup(file, 'html.parser')  # 使用BeautifulSoup库解析网页内容
            item = soup.find_all("span",class_="skipped")[0].string  # 使用BeautifulSoup库的标签方法找到你需要的内容
            return str(item)

    def get_failed(self):
        with open(html_path, "r", encoding="utf-8") as f:
            file = f.read()
            soup = BeautifulSoup(file, 'html.parser')  # 使用BeautifulSoup库解析网页内容
            item = soup.find_all("span",class_="failed")[0].string  # 使用BeautifulSoup库的标签方法找到你需要的内容
            return str(item)


    def get_error(self):
        with open(html_path, "r", encoding="utf-8") as f:
            file = f.read()
            soup = BeautifulSoup(file, 'html.parser')  # 使用BeautifulSoup库解析网页内容
            item = soup.find_all("span",class_="error")[0].string  # 使用BeautifulSoup库的标签方法找到你需要的内容
            return str(item)


    def get_xfailed(self):
        with open(html_path, "r", encoding="utf-8") as f:
            file = f.read()
            soup = BeautifulSoup(file, 'html.parser')  # 使用BeautifulSoup库解析网页内容
            item = soup.find_all("span",class_="xfailed")[0].string  # 使用BeautifulSoup库的标签方法找到你需要的内容
            return str(item)

    def get_xpassed(self):
        with open(html_path, "r", encoding="utf-8") as f:
            file = f.read()
            soup = BeautifulSoup(file, 'html.parser')  # 使用BeautifulSoup库解析网页内容
            item = soup.find_all("span",class_="xpassed")[0].string  # 使用BeautifulSoup库的标签方法找到你需要的内容
            return str(item)

if __name__ == '__main__':
    t = GetHtml()
    t.get_xpassed()

如下代码:
发送钉钉消息的模块:


import requests
import json
from common.handle_readhtml import GetHtml

class SendMassage:

    """
    发送测试结果到钉钉群
    """
    result = GetHtml()
    total = result.get_total()
    passed = result.get_pass()
    skipped = result.get_skipped()
    failed = result.get_failed()
    error = result.get_error()
    xfailed = result.get_xfailed()
    xpassed = result.get_xpassed()
    def send_text(self):

        url = "https://oapi.dingtalk.com/robot/send?access_token=43c4dab2ac3152e605c458b4b9561a73"
        headers = {'Content-Type': 'application/json'}
        data = {"at": {"atMobiles":["18206233880"],"atUserIds":["user123"],"isAtAll": False},
                "text": {"content":"砍价小程序接口自动化测试 \n total       : {}\n passed   : {},\n skipped : {},\n failed    : {},\n error     : {},\n xfailed   : {},\n xpassed : {}".format(self.total,self.passed,self.skipped,self.failed,self.error,self.xfailed,self.xpassed)},"msgtype":"text"}
        requests.post(url,headers=headers,data=json.dumps(data))


if __name__ == '__main__':
    s = SendMassage()
    s.send_text()

jenkins配置的shell为:
先执行接口自动化脚本,等待一会然后发送钉钉消息;

${PYTHON} main.py
sleep 100
${PYTHON} handle_dingding.py

接口自动化发钉钉群消息还可以再优化,比如可以加上断言失败的错误日志等;
在这里插入图片描述

2,监控qa环境错误日志
这里借用大佬的一篇博客:https://www.cnblogs.com/zy0209/p/12769466.html
此处发送的qq邮件,消息查看不方便,且不好共享,可以优化为发钉钉群消息,然后将开发也拉到群里,提高效率;
3,jira上有钉钉机器人插件,可以每天发送消息@某某开发 还有N个待处理bug,@某某测试 还有N个待验证bug,以及监控看板指标达到阈值报警等;

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐