自动化测试-Nodejs + mocha + mocha-allure-reporter Trend部分没有值的问题

问题描述

allure初步使用可以参照这个文章
链接: mocha-allure-reporter使用.
接下来描述的遇到的问题:在打开allure报告的时候发现trend部分没有值,
这个部分应该记录着历史执行过的数据,每次执行的成功失败的次数,但是因为每次都是重新生成report,所以记录历史数据的文件每次都被更新,所以导致这个部分没有数据
allure 报告

解决方案

由于从执行测试文件,到生成打开报告,共需要三步走,所以想到了一个初步的执行方案:
1.先保存上一次的文件的值
在这里插入图片描述
2.执行测试文件
3.生成report
4.打开report

解决过程

写一个执行文件execute.js去把上述过程一起执行

const { execSync } = require('child_process');
const path = require("path")
const fs = require("fs")
const iconv = require('iconv-lite');
let executeJson = {
    "test": {
        "executePath": "xxxx.js",
        "allure-results": "xxxx",
        "allure-report": "xxxx"
    }
}

// node xxx.js test 可获取到test的值
var data = process.argv.splice(2);
console.log('传入参数:',data);

// 删除allure-results下的文件
function deleteAll(path = executeJson[data[0]]['allure-results']) {

    let files = [];
    if (fs.existsSync(path)) {
        files = fs.readdirSync(path);
        files.forEach(function (file, index) {
            let curPath = path + "/" + file;
            if (fs.statSync(curPath).isDirectory()) { // recurse
                deleteall(curPath);
            } else { // delete file
                fs.unlinkSync(curPath);
            }
        });
        fs.rmdirSync(path);
    }
}

// 保存历史文件
function saveHistoryFile(){
    let historyFile;
    if (fs.existsSync(path.join(executeJson[data[0]]["allure-report"],"widgets\\history-trend.json"))){
        historyFile = JSON.parse(iconv.decode(new Buffer(fs.readFileSync(path.join(executeJson[data[0]]["allure-report"],"widgets\\history-trend.json")),"binary"),"utf8"));
    }

    return historyFile;
}

// 更新历史文件
function updateHistoryFile(historyFile){
    let newFile = JSON.parse(iconv.decode(new Buffer(fs.readFileSync(path.join(executeJson[data[0]]["allure-report"],"widgets\\history-trend.json")),"binary"),"utf8"));
    console.log("historyFile",historyFile)
    if (historyFile == undefined){
        historyFile = [];
        console.log("historyFile",historyFile)
    }
    historyFile.unshift(newFile[0])
    console.log(historyFile)
    fs.writeFileSync(path.join(executeJson[data[0]]["allure-report"],"widgets\\history-trend.json"),JSON.stringify(historyFile));
}


if(executeJson.hasOwnProperty(data[0])){
	deleteAll()
		// 保存历史数据
    let historyFile = saveHistoryFile();
    // 执行测试文件
    execSync('mocha  '+executeJson[data[0]]["executePath"]+'  --reporter mocha-allure-reporter');
		// 生成报告
    execSync('allure generate '+executeJson[data[0]]["allure-results"]+' --clean -o '+ executeJson[data[0]]["allure-report"]);
		// 更新新的历史文件
    updateHistoryFile(historyFile)
		// 打开报告
    execSync('allure open '+ executeJson[data[0]]["allure-report"]);

}else{
    console.log("No need to execute");
}

效果图
在这里插入图片描述

代码写的比较粗糙,大概思路是这样,欢迎交流。
期待学习的路上不断成长!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值