使用摩卡javascript测试框架,我希望能够在先前定义的测试通过后执行多个测试(所有异步)。只有先前的异步测试通过后,我如何才能运行摩卡测试?
我不想将这些测试嵌套在一起。
describe("BBController", function() {
it("should save", function(done) {});
it("should delete", function(done) {});
})
使用摩卡javascript测试框架,我希望能够在先前定义的测试通过后执行多个测试(所有异步)。只有先前的异步测试通过后,我如何才能运行摩卡测试?
我不想将这些测试嵌套在一起。
describe("BBController", function() {
it("should save", function(done) {});
it("should delete", function(done) {});
})
如果您的测试设置正确,只测试一小块业务逻辑,那么您可以异步运行测试,但它们不应该阻止其他测试。得到测试完成的方法是做到以下几点:
describe("BBController", function() {
it("should save", function(done) {
// handle logic
// handle assertion or other test
done(); //let mocha know test is complete - results are added to test list
});
it("should delete", function(done) {
// handle logic
// handle assertion or other test
done(); //let mocha know test is complete - results are added to test list
});
});
再次,没有测试应该需要等待另一个测试运行通过,如果你有这样的问题,那么你应该着眼于如何impove您的依赖注射或使用before方法准备测试
使用--bail
选项。确保你至少使用摩卡0.14.0。 (我已经用老版本尝试过了,但没有成功。)
首先,没有什么需要摩卡才能在上一次测试完成后才能运行测试。摩卡咖啡就是这样默认的。这个保存到test.js
:
describe("test", function() {
this.timeout(5 * 1000); // Tests time out in 5 seconds.
it("first", function (done) {
console.log("first: do nothing");
done();
});
it("second", function (done) {
console.log("second is executing");
// This test will take 2.5 seconds.
setTimeout(function() {
done();
}, 2.5 * 1000);
});
it("third", function (done) {
console.log("third is executing");
// This test will time out.
});
it("fourth", function (done) {
console.log("fourth: do nothing");
done();
});
});
然后执行它:
mocha -R spec test.js
你不会看到第四测试开始到:
现在,运行它:
mocha -R spec --bail test.js
摩卡会尽快测试3失败停止。
你只是说测试不应该像我问的那样构造,它不会回答问题。另外,你的代码只需调用done(),这与我所要求的设置任何类型的依赖关系无关。我假设你真正想说的是摩卡不支持这种形式的依赖。 –
不以任何方式回答问题......正如Tim所说。完成与此问题无关。 – mbochynski
答案指出测试没有正确设置,没有测试应该依赖任何其他测试运行有效。任何需要运行测试的设置都应该在before块中运行,事实上,以随机顺序运行测试以确保您没有像那样的测试依赖关系是个好习惯 – kkemple