其他分享
首页 > 其他分享> > mochajs框架发送报告到Slack和企业微信

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))
}

标签:totalTestNumber,console,Slack,mochajs,微信,let,passedNum,report,log
来源: https://blog.csdn.net/qq_28207005/article/details/118421530