繁体   English   中英

如何在 mocha.js(或其他库)中修改测试结果的 output

[英]How to modify test results' output in mocha.js (or another library)

我正在创建一个将由 mocha 运行的测试套件。 在测试中,我打算至少使用shouldchai 我正在测试 JSON 对象。 我希望 mocha 对成功的测试保持沉默,并且我想控制运行结束时错误的样子。 现在有很多红字描述JSON object等信息。 我想要的是覆盖 output。

这是 JSON object 中的元素的示例测试,我将从testData导入。

var should = require("should");
var chai = require("chai");
var expect = chai.expect;

const testData = require("./testData");

for (t of testData.data) {
        describe("This will fail", function() {

                it("Should have a zipperNut",function(done) {

                        t.should.have.property('zipperNut');
                        done();
                });
        });
}

我想要的是看到这个:

$ mocha testSuite.js
zipperNut
$

等等我想运行的所有测试。 我可以这样做吗? 我需要使用不同的库吗?

Mocha 的 output 由称为“默认报告器”的东西确定。 如果你想改变 output 你可以指定一个不同的报告器,这可能是通过 npm 或类似的。 要指定新的自定义报告器,请执行以下操作:

$ mocha --reporter my-reporter.js

其中 my-reporter.js 是包含我的“记者”的文件。 对我来说,我只想要失败测试的简单名称,所以我的记者看起来像:

'use strict';

const Mocha = require('mocha');
const {
  EVENT_RUN_END,
  EVENT_TEST_FAIL,
} = Mocha.Runner.constants;

class MyReporter {
  constructor(runner) {
    const stats = runner.stats;

    runner
      .on(EVENT_TEST_FAIL, (test, err) => {
        console.log(
          `${test.title}`
        );
      })
      .once(EVENT_RUN_END, () => {
        console.log(`end: ${stats.passes}/${stats.passes + stats.failures} ok`);
      });
  }
}

module.exports = MyReporter;

此处的文档:

https://mochajs.org/api/tutorial-custom-reporter.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM