我想知道是否有一种方法可以让 mocha 列出它将执行的所有测试。当我使用 mocha --help; 列出它们时,我没有看到任何合理的选项;有几个报告器,但似乎没有一个旨在列出将要处理的文件(或命名将要运行的测试)。
报告器的工作方式是监听 mocha 调度的事件,这些事件仅在运行实际测试时调度。
套件包含测试列表,因此有您需要的信息。然而,该套件通常仅在 run() 上初始化。
如果您可以从nodejs而不是从命令行运行mocha,您可以根据Using-mocha-programmatically中的代码创建这个diy解决方案:
var Mocha = require('mocha'),
fs = require('fs'),
path = require('path');
// First, you need to instantiate a Mocha instance.
var mocha = new Mocha(),
testdir = 'test';
// Then, you need to use the method "addFile" on the mocha
// object for each file.
// Here is an example:
fs.readdirSync(testdir).filter(function(file){
// Only keep the .js files
return file.substr(-3) === '.js';
}).forEach(function(file){
// Use the method "addFile" to add the file to mocha
mocha.addFile(
path.join(testdir, file)
);
});
// Here is the code to list tests without running:
// call mocha to load the files (and scan them for tests)
mocha.loadFiles(function () {
// upon completion list the tests found
var count = 0;
mocha.suite.eachTest(function (t) {
count += 1;
console.log('found test (' + count + ') ' + t.title);
})
console.log('done');
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1310 次 |
| 最近记录: |