【发布时间】:2021-09-19 09:53:01
【问题描述】:
我在启用 json 报告器的情况下运行我的 mocha 测试。 使用 API 完成我的套件后,我需要将结果发布到测试管理工具。 为此,我正在尝试读取我的输出 json 文件并了解哪些测试通过/失败。因此,我正在创建一个 API Payload 以在测试管理工具上发布结果。 为此,我已将我的代码块包含在测试文件的“After”块中读取输出 json,但问题是在处理“After”块时未生成报告。
下面是示例代码
Test.js:
require('dotenv').config();
const commandLineArgs = require('command-line-args');
const request = require('request').defaults({rejectUnauthorized: false});
const fs = require('fs');
const optionDefinitions = [
{name: 'file', alias: 'f', type: String},
{name: 'format', alias: 'm', type: String, defaultValue: 'newman-json'},
{name: 'usetestcaseid', alias: 'i', type: String},
{name: 'regex', alias: 'r', type: String, defaultValue: /^[^\d]*(\d+)/},
{name: 'parentid', alias: 'p', type: String},
{name: 'parenttype', alias: 't', type: String, defaultValue: 'root'},
{name: 'help', alias: 'h', type: Boolean},
];
const options = commandLineArgs(optionDefinitions);
var assert = require('assert');
it('12345_should return -1 when the value is not present', function() {
assert.equal([1, 2, 3].indexOf(4), -1);
});
after(function publishReport() {
// runs once after the last test in this block
var outputReport = fs.readFileSync('filename.json', 'utf8');
var rpt=JSON.parse(outputReport);
console.log(rpt);
var passedCases = rpt.passes;
console.log(passedCases);
});
配置文件:
{
"spec": "test/**/test1.js",
"reporter": "mochajs/json-file-reporter",
"reporter-option": [
"output=filename.json"
]
}
任何人都可以建议我像我需要将我的代码准确地放在 Mocha 上的哪个位置,以便我可以在生成后读取输出报告文件并发布结果吗?
【问题讨论】:
标签: mocha.js