【问题标题】:Read Mocha json report and publish the result to a Test Management tool阅读 Mocha json 报告并将结果发布到测试管理工具
【发布时间】: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


    【解决方案1】:

    mocha(及其记者)给出的唯一保证是输出文件存在于mocha process 退出之后

    因此,您必须将代码放入单独的脚本(例如,upload.js)并像这样运行测试套件(package.json 中的条目):

    "tests-and-upload-results:": "mocha ; node ./upload.js"
    

    所有钩子都在测试运行范围内执行,并且报告器一般在整个测试运行完成之前不会生成报告。

    【讨论】:

    • 谢谢@Zbigniew。正如建议的那样,我创建了一个单独的 .js 文件来读取 json 报告并发布结果。现在,我在 package.json 中的脚本部分看起来像 "scripts": { "test": "mocha", "tests-and-upload-results": "npm test ./publishResultsToqTest.js" } 但是当我运行 npm test只有测试正在运行,publishResultsToqTest.js 没有被执行。我错过了什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多