问题描述
allure初步使用可以参照这个文章
链接: mocha-allure-reporter使用.
接下来描述的遇到的问题:在打开allure报告的时候发现trend部分没有值,
这个部分应该记录着历史执行过的数据,每次执行的成功失败的次数,但是因为每次都是重新生成report,所以记录历史数据的文件每次都被更新,所以导致这个部分没有数据
解决方案
由于从执行测试文件,到生成打开报告,共需要三步走,所以想到了一个初步的执行方案:
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");
}
效果图
代码写的比较粗糙,大概思路是这样,欢迎交流。
期待学习的路上不断成长!