mochajs框架发送报告到Slack和企业微信

//环境变量相关的
let configENV = XXXXX
let totalTestNumber = 0;
let passedNum = 0;
let reportURL = configENV.reportURL
let wxHook = configENV.wxHook
let slackHook = configENV.slackHook
let testEnv = configENV.testEnv  //测试环境信息
// 统计运行测试个数
afterEach(async function () {
    if (this.currentTest.state === "passed") {
        passedNum = passedNum + 1
    }
    totalTestNumber = totalTestNumber + 1
    console.log(totalTestNumber, ":", this.currentTest.title, "--<run status>--", this.currentTest.state)
})


// 统计结果并发送消息
after(async function () {
    try {
        if (wxHook && reportURL) {
            console.log("send wx report")
            await sendWXReport(wxHook)
        }
        if (slackHook && reportURL) {
            let slackToken = configENV.slackHook.token
            let slackChannel = configENV.slackHook.channel
            await sendSlackReport(slackToken, slackChannel)
            console.log("send slack report")
        }
    }
    catch (err) {
        console.log(err)
    }
})


/**
 * 向企业微信发送测试报告
 * @param {*} wxURL 
 * @param {*} reportURL 
 * @returns 
 */
function sendWXReport(wxURL) {
    return new Promise(function (resolve, reject) {
        request.post({
            url: wxURL,
            body: JSON.stringify({
                "msgtype": "markdown",
                "markdown": {
                    "content": `API TEST RESULT
                    >Total: ${totalTestNumber} Pass: ${passedNum} Fail: ${totalTestNumber - passedNum}
                    >env: ${testEnv}
                    >report:[report url](${reportURL})`
                }
            })
        }
        ), function (err, responded, body) {
            if (err) {
                console.log('send report failed', err);
                console.log(err)
                reject(err)
            }
            console.log('send report successful!  Server responded with:', body);
            resolve(body)
        }
    })
}


/**
 * 向slack指定channel发送消息
 * @param {*} token 
 * @param {*} channel 
 */
async function sendSlackReport(token, channel) {
    const web = new WebClient(token);
    let result = await web.chat.postMessage({
        "type": "section",
        "channel": channel,
        "text": {
            "type": "mrkdwn",
            "text": `API TEST RESULT:\n
            \tTotal:${totalTestNumber} Pass:${passedNum} Fail: ${totalTestNumber - passedNum} \n
            \tenv: ${testEnv}\n
            \treport:[report url](${reportURL})
            `
        }
    });
    console.log(JSON.stringify(result))
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

走走又停停,人生路上皆风景

不同的你我,不同的风景,祝幸福

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

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

打赏作者

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

抵扣说明:

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

余额充值