简体   繁体   English

仅在先前的异步测试通过后,如何运行Mocha测试?

[英]How do I run a mocha test only after the prior asynchronous test has passed?

Using the mocha javascript testing framework, I want to be able to have several tests (all asynchronous) only execute after the previously defined test has passed. 使用mocha javascript测试框架,我希望能够仅在先前定义的测试通过后才能执行多个测试(全部异步)。

I don't want to have to nest these tests within each other. 我不想将这些测试相互嵌套。

describe("BBController", function() {
    it("should save", function(done) {});
    it("should delete", function(done) {});
})

Use the --bail option. 使用--bail选项。 Make sure you are using at least mocha 0.14.0. 确保您至少使用了摩卡咖啡0.14.0。 (I've tried it with older versions without success.) (我已经在较旧的版本中尝试过,但是没有成功。)

First, there's nothing you need to do for mocha to run a test only after the previous one has finished. 首先,仅在上一个测试完成后,您无需做任何事情就可以进行摩卡测试。 That's how mocha works by default. 默认情况下,这就是mocha的工作方式。 Save this to test.js : 保存到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();
    });
});

Then execute it with: 然后执行:

mocha -R spec test.js

You will not see the fourth test start until: 在以下情况下,您将看不到第四个测试开始:

  1. The first and second tests are finished. 第一次和第二次测试已完成。
  2. The third tests has timed out. 第三项测试已超时。

Now, run it with: 现在,运行:

mocha -R spec --bail test.js

Mocha will stop as soon as test 3 fails. 测试3失败后,摩卡咖啡将立即停止。

if your tests are set up properly, only testing a small piece of business logic then you can run your tests asynchronously but they should not hold up other tests. 如果您的测试设置正确,仅测试一小部分业务逻辑,则可以异步运行测试,但它们不应阻止其他测试。 the way to get a test to complete is to do the following: 完成测试的方法是执行以下操作:

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
    });
});

again, no test should need to wait for another test to run to pass, if you have this issue then you should look at ways to impove your dependency injection or prep your tests with a before method 再次,没有测试应该等待另一个测试通过,如果您遇到此问题,则应考虑改善依赖注入或使用before方法准备测试的方法

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

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