这里写目录标题
allure下载和应用
基于mocha框架进行的自动化测试,如果需要allure报告的格式需要先下载mocha-allure-reporter
// 首先nodejs环境都已经安装好
npm install mocha-allure-reporter
install之后,就会生成一个全局变量’allure ',这个变量可以调用它所提供的方法,具体可以参考
链接: allure-mocha-reporter.
1.当自动测测试脚本都完成之后,可以执行测试脚本
mocha xxx.js --reporter mocha-allure-reporter
2.执行完测试脚本之后,会生成一个allure-result的文件夹,之后运行生成报告的语句
// 生成报告
allure generate allure-results --clean -o allure-report
3.当提示生成report成功时,会生成一个allure-report的文件夹,文件夹结构如下
4.如果需要在浏览器上打开报告,我用了两种方式
(1)我用的idea,打开index.html文件,选择一个浏览器进行打开
(2)命令行
// 生成报告
allure open allure-report
之后就会在浏览器打开报告,大概的报告的样子如下
设置参数
报告中,可能需要自己去修改一部分内容,我根据自己的需求,稍作改动
需求:发送API的时候,需要记录每次发送API的时间,request,response
解决方案:mocha框架有自己的Hooks,我用到了beforeEach(),afterEach()(在mocha框架里面,分别在每一个it测试前后执行)
1.定义request,response变量
2.在api发送的时候把信息赋值给变量,之后保存在allure里面,具体实现代码如下
beforeEach(function () {
request = null
response = null;
// Util.getDataFormat就是自己定义一个格式时间的方法
allure.createAttachment("测试开始时间", Util.getDataFormat(new Date(), "YYYY-MM-DD HH:mm:ss.SSS"),allureCommon.ContentType.TEXT);
});
afterEach(function () {
if(request != null){
allure.createAttachment("request", JSON.stringify(request),allureCommon.ContentType.JSON);
allure.createAttachment("response", JSON.stringify(response),allureCommon.ContentType.JSON);
}
allure.createAttachment("测试结束时间", Util.getDataFormat(new Date(), "YYYY-MM-DD HH:mm:ss.SSS"),allureCommon.ContentType.TEXT);
allure.createAttachment("截图", new Buffer(fs.readFileSync("src\\web\\screenshot\\02_menu.png"),"base64"),allureCommon.ContentType.PNG);
});
其中 allureCommon.ContentType.JSON 这个可以直接写对应的字符串。或者npm install allure-js-commons 之后直接调用
export declare enum ContentType {
TEXT = "text/plain",
XML = "application/xml",
CSV = "text/csv",
TSV = "text/tab-separated-values",
CSS = "text/css",
URI = "text/uri-list",
SVG = "image/svg+xml",
PNG = "image/png",
JSON = "application/json",
ZIP = "application/zip",
WEBM = "video/webm",
JPEG = "image/jpeg"
}
效果如下
其他方法也基本类似,可以尝试一下
本地查看报告的方式
当把allure-report文件夹复制到本地时,再打开的时候,报告页面会没有数据,或者404等错误,是因为直接打开没有allure渲染,无法打开
所以可以才有一个别的方法
我才用的是写一个sh文件,比如openReport.sh
文件的内容如下
之后双击这个sh文件。就会自己打开浏览器,能展示正常的报告了