简体   繁体   English

从Mocha获取所有测试失败

[英]Get all test fails from Mocha

So, I have about 600 tests made with Mocha and now I need to automatically run all of them and get all errors and success count to send this information to monitoring server. 因此,我用Mocha进行了约600项测试,现在我需要自动运行所有这些测试,并获取所有错误和成功计数,才能将该信息发送到监视服务器。

I can make bash script, which runs tests and writes Mocha log to file, then parse this log file and get success count and fail logs (for example by grep), but this is too dirty solution. 我可以制作bash脚本,该脚本运行测试并将Mocha日志写入文件,然后解析此日志文件并获取成功计数和失败日志(例如,通过grep),但这太脏了。

I would prefer run tests programmatically and get from Mocha something like fail messages and success tests array to work around this, but I couldn't find any docs about this. 我更喜欢以编程方式运行测试,并从Mocha获得诸如失败消息和成功测试数组之类的内容来解决此问题,但是我找不到有关此的任何文档。

So, how to solve this problem? 那么,如何解决这个问题呢?

Create a file, let's say intercept-failures.js with the following content: 创建一个文件,假设它具有以下内容: intercept-failures.js

const failures = [];
const successes = [];

afterEach(function () {
    const title = this.currentTest.title;
    const state = this.currentTest.state;
    if (state === "passed") {
        successes.push(title)
    } else if (state === "failed") {
        failures.push(title)
    }
});

after(function () {
    console.log("failures", failures);
    console.log("successes", successes);
});

Add a flag --file intercept-failures.js to your mocha invocation (for example mocha --file intercept-failures.js test/** ) 在您的Mocha调用中添加标志--file intercept-failures.js -failures.js(例如mocha --file intercept-failures.js test/**

The afterEach hook accumulates all test results, and then you can do something with them in the after hook. afterEach挂钩会累积所有测试结果,然后您可以在after挂钩中对其进行操作。 The --file flag just makes sure that the hooks are added to all test suites. --file标志只是确保将钩子添加到所有测试套件中。

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

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