eri*_*icP 11
你不能直接用mocha做这个,因为它创建了一个it()回调列表并按顺序调用它们. 如果您愿意将描述移动到单独的.js文件中,则mocha-parallel-tests可以执行此操作.为了说服自己,将它安装在某处并使用short -slow调用它,以便每次都报告:
laptop:/tmp/delme$ npm install mocha-parallel-tests
laptop:/tmp/delme$ cd node_modules/mocha-parallel-tests
laptop:/tmp/delme/node_modules/mocha-parallel-tests$ ./bin/mocha-parallel-tests test/parallel/tests --timeout 10000 --slow 100
Run Code Online (Sandbox Code Playgroud)
您将看到它在运行时间最长的时间内运行了三个(非常简单的)测试套件.
如果您的测试不依赖于早期测试的副作用,则可以使它们全部异步.一个简单的方法是在描述之前启动需要一段时间的东西并使用常规的mocha设备来评估它.在这里,我创建了一堆承诺,需要一段时间才能解决,然后再次遍历测试,在.then()函数中检查它们的结果:
var expect = require("chai").expect;
var SlowTests = [
{ name: "a" , time: 250 },
{ name: "b" , time: 500 },
{ name: "c" , time: 750 },
{ name: "d" , time:1000 },
{ name: "e" , time:1250 },
{ name: "f" , time:1500 }
];
SlowTests.forEach(function (test) {
test.promise = takeAWhile(test.time);
});
describe("SlowTests", function () {
// mocha defaults to 2s timeout. change to 5s with: this.timeout(5000);
SlowTests.forEach(function (test) {
it("should pass '" + test.name + "' in around "+ test.time +" mseconds.",
function (done) {
test.promise.then(function (res) {
expect(res).to.be.equal(test.time);
done();
}).catch(function (err) {
done(err);
});
});
});
});
function takeAWhile (time) {
return new Promise(function (resolve, reject) {
setTimeout(function () {
resolve(time);
}, time);
});
}Run Code Online (Sandbox Code Playgroud)
foo.js并调用mocha foo.js.)
Meta I不同意测试应主要是同步的断言.在编译指示之前和之后更容易,但是很少有一个测试使所有剩余的测试无效.所有令人沮丧的异步测试确实阻止了对网络任务的广泛测试.
只是为了更新这个问题,Mocha 版本 8+ 现在原生支持并行运行。您可以使用该--parallel标志并行运行测试。
对于许多用例来说,并行测试应该是开箱即用的。但是,您必须意识到该行为的一些重要含义
需要注意的一件事是,一些记者目前不支持此执行(例如mocha-junit-reporter )
| 归档时间: |
|
| 查看次数: |
13688 次 |
| 最近记录: |