简体   繁体   English

Mocha与done()回调并行运行异步测试

[英]Mocha run async tests in parallel with done() callback

I'm running some asynchronous tests with Mocha, but some future tests can't be executed until previous ones are completed. 我正在使用Mocha运行一些异步测试,但是在完成之前的测试之前,无法执行某些将来的测试。 For this, I can simply use done() callback to run them synchronously: 为此,我可以简单地使用done()回调来同步运行它们:

describe('first operations', function() {
    it('should do something', function(done) {
        longOperation(function(err) {
            done();
        });
    });
    it('should do something', function(done) {
        longOperation(function(err) {
            done();
        });
    });
});

describe('second operations', function() {
    it('should do something', function(done) {
        longOperation(function(err) {
            done();
        });
    });
    it('should do something', function(done) {
        longOperation(function(err) {
            done();
        });
    });
});

Doing so though, slows down the entire operation because I'm stuck running each it() block synchronously. 但是这样做会减慢整个操作的速度,因为我一直在同步运行每个it()块。 I'd like to run the inner tests asynchronously, and each describe block synchronously, but the done() callback doesn't seem to work that way (at least, using async ). 我想异步运行内部测试,每个测试都同步描述块,但是done()回调似乎没有这种方式(至少使用async )。

Am I doing something wrong, or is that simply not supported? 我是在做错什么,还是根本不支持? If not, is there a way I can get around this to accomplish my goal? 如果不是,是否有办法可以解决这个问题以实现我的目标?

describe('first operations', function(done) {
    async.parallel([
        function(callback) {
            it('should do something', function() {
                longOperation(function(err) {
                    callback();
                });
            });
        },
        function(callback) {
            it('should do something', function() {
                longOperation(function(err) {
                    callback();
                });
            });
        }
    ], function(err) {
        done();
    });
});

describe('second operations', function(done) {
    async.parallel([
        function(callback) {
            it('should do something', function() {
                longOperation(function(err) {
                    callback();
                });
            });
        },
        function(callback) {
            it('should do something', function() {
                longOperation(function(err) {
                    callback();
                });
            });
        }
    ], function(err) {
        done();
    });
});

mocha-parallel-tests seems to have been created to fill this need. 似乎已经创建了mocha-parallel-tests来满足这一需求。 Here's a description from the project site: 这是项目站点的描述:

Normally tests written with mocha run sequentially. 通常,用mocha编写的测试是按顺序运行的。 This happens so because each test suite should not depend on another. 发生这种情况是因为每个测试套件都不应该依赖另一个。 But if you are running tests which take a lot of time (for example tests with Selenium Webdriver) waiting for so much time is impossible. 但是,如果您运行的测试花费大量时间(例如,使用Selenium Webdriver进行的测试),那么等待这么长时间是不可能的。

I believe this will run all tests in parallel though. 我相信这将并行运行所有测试。 To limit the describe blocks to run sequentially you could put them in individual files and then run mocha-parallel-tests separately on these files. 要限制describe块按顺序运行,可以将它们放在单独的文件中,然后分别在这些文件上运行mocha-parallel-tests

context.describe() doesn't seem to be an instance of Runnable . context.describe()似乎不是Runnable的实例。 context.it() , on the other hand seems to be creating an instance of Runnable . 另一方面, context.it()似乎正在创建Runnable的实例。 It looks like only instances of Runnable receive a callback . 看起来只有Runnable实例才收到回调

So no, it looks like you cannot run describe() blocks serially while running enclosed it() blocks asynchronously. 因此,不,您似乎无法在异步运行封闭的it()块时连续运行describe()块。

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

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