pytest 用例收集钩子并发送飞书

pytest 用例收集钩子并发送飞书

当我们pytest用例运行完成后,想要知道运行多少个,成功还是失败,分别是多少,直接通知领导或者发到群里

conftest.py

import requests
def pytest_terminal_summary(terminalreporter, exitstatus, config):  # noqa
    """收集测试结果"""
    total = terminalreporter._numcollected  # noqa
    if total > 0:
        passed = len([i for i in terminalreporter.stats.get('passed', []) if i.when != 'teardown'])
        failed = len([i for i in terminalreporter.stats.get('failed', []) if i.when != 'teardown'])
        error = len([i for i in terminalreporter.stats.get('error', []) if i.when != 'teardown'])
        skipped = len([i for i in terminalreporter.stats.get('skipped', []) if i.when != 'teardown'])
        if terminalreporter._numcollected - skipped == 0:  # noqa
            successful = 0
        else:
            successful = len(terminalreporter.stats.get('passed', [])) / terminalreporter._numcollected * 100  # noqa
        duration = time.time() - terminalreporter._sessionstarttime  # noqa
        # 发送飞书消息
        send_feishu(total, passed, failed, error, skipped, successful)
        print(
            f'测试用例总数:{total},成功:{passed},失败:{failed},错误:{error},跳过:{skipped},成功率:{successful}%,耗时:{duration:.2f}s')


def send_feishu(total, passed, failed, error, skipped, successful):
    # 设置飞书群机器人的 Webhook URL
    webhook_url = ''  # 替换为你的 Webhook URL

    # 设置消息内容
    data = {
        "msg_type": "post",
        "content": {
            "post": {
                "zh_cn": {
                    "title": "车控自动化测试",
                    "content": [
                        [{
                            "tag": "text",
                            "text": f"执行人: O_zhenhua.zhang\n"
                        },
                            {
                                "tag": "text",
                                "text": f"执行结果: 测试用例总数:{total}\n"
                            },
                            {
                                "tag": "text",
                                "text": f"成功:{passed}\n"
                            },
                            {
                                "tag": "text",
                                "text": f"失败:{failed}\n"
                            },
                            {
                                "tag": "text",
                                "text": f"错误:{error}\n"
                            },
                            {
                                "tag": "text",
                                "text": f"跳过:{skipped}\n"
                            },
                            {
                                "tag": "text",
                                "text": f"成功率:{successful}%\n"
                            },
                            {
                                "tag": "a",
                                "text": "请点击这里查看\n",
                                "href": "报告地址\n"
                            },
                            {
                                "tag": "at",
                                "user_id": "all"
                            }
                        ]
                    ]
                }
            }
        }
    }

    # 发送消息到飞书群机器人
    headers = {'Content-Type': 'application/json'}  # 设置请求头为 JSON 类型
    response = requests.post(webhook_url, headers=headers, json=data)

    # 检查响应状态码,如果是 200 则表示发送成功
    if response.status_code == 200:
        print("报告发送成功!")
    else:
        print(f"报告发送失败,状态码:{response.status_code},错误信息:{response.text}")
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

车载testing

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值