简体   繁体   English

可配置数量的测试失败后,停止Mocha测试套件的执行

[英]Stopping Mocha test suite's execution after a configurable number of test failures

I want to stop my test suite after a configurable number of tests fails. 可配置数量的测试失败后,我想停止测试套件。 My test suite has different files containing Mocha tests. 我的测试套件具有包含Mocha测试的不同文件。

I want to take input from user in order to know after how many test case failures do they want to stop execution of the complete test suite. 我想从用户那里获取输入,以便知道他们要停止执行完整的测试套件之后发生多少个测试用例失败。 So if the users gives 5 as an input, my test suite should stop after the 5th test cases fail. 因此,如果用户输入5,则我的测试套件应在第5个测试用例失败后停止。

Can anyone help me if figuring out how this can be done? 如果弄清楚如何做到这一点,谁能帮助我?

Here's an attempt: 这是一个尝试:

// mocha-plugin-fail-after.js
const { Runner } = require('mocha');
const fail       = Runner.prototype.fail;
const opts       = require('minimist')(process.argv);
const failAfter  = Number(opts['fail-after'] || 5);

let failures = 0;
Runner.prototype.fail = function() {
  if (++failures > failAfter) {
    console.log('\nToo many failures, exiting...');
    process.exit(failures);
  }
  return fail.apply(this, arguments);
}

To use: 使用方法:

mocha -r mocha-plugin-fail-after --fail-after=5 test/**/*.js

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

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