简体   繁体   English

如何按定义的顺序运行Mocha测试

[英]How to run Mocha tests in a defined order

Background 背景

I am working on a program in Node.js and writing my test suites in Mocha with Chai and SinonJS. 我正在研究Node.js中的一个程序,并使用Chai和SinonJS在Mocha中编写我的测试套件。 I have core graphics module which controls access to a node-webgl context. 我有核心图形模块,它控制对node-webgl上下文的访问。

Due to how node-webgl works, I only wish to initialize a context once for the entire test run. 由于node-webgl工作原理,我只希望为整个测试运行初始化一次上下文。 I have some tests I wish to run prior to the initialization of the core module, like so: 我想在核心模块初始化之前运行一些测试,如下所示:

describe('module:core', function () {
    describe('pre-init', function () {
        describe('.isInitialized', function () {
            it('should return false if the module is not initialized', function () {
                expect(core.isInitialized()).to.be.false;
            });
        });
        describe('.getContext', function () {
            it('should error if no context is available', function () {
                expect(function () {
                    core.getContext();
                }).to.throw(/no context/i);
            });
        });
    });
    describe('.init', function () {
        it('should error on an invalid canvas', function () {
            expect(function () {
                core.init(null);
            }).to.throw(/undefined or not an object/i);
            expect(function () {
                core.init({});
            }).to.throw(/missing getcontext/i);
        });
        it('should error if the native context could not be created', function () {
            var stub = sinon.stub(global._canvas, 'getContext').returns(null);
            expect(function () {
                core.init(global._canvas);
            }).to.throw(/returned null/i);
            stub.restore();
        });
        it('should initialize the core module', function () {
            expect(function () {
                core.init(global._canvas);
            }).not.to.throw();
        });
    });
    describe('post-init', function () {
        describe('.isInitialized', function () {
            it('should return true if the module is initialized', function () {
                expect(core.isInitialized()).to.be.true;
            });
        });
        describe('.getContext', function () {
            it('should return the current WebGL context', function () {
                var gl = null;
                expect(function () {
                    gl = core.getContext();
                }).not.to.throw();
                // TODO Figure out if it's actually a WebGL context.
                expect(gl).to.exist;
            });
        });
    });
});

Then I can run the remaining tests. 然后我可以运行剩余的测试。

Problem 问题

When I run this through Mocha, everything is fine since the core test suite is the first thing to be run. 当我通过Mocha运行时,一切都很好,因为核心测试套件是第一个运行的东西。 My concern is that if any test suites get run before the core test suite, then those test suites will fail as the core is not initialized yet. 我担心的是,如果任何测试套件在核心测试套件之前运行,那么那些测试套件将会因为核心尚未初始化而失败。

What is the best way to ensure the core test suite is always run before any other test suites? 确保核心测试套件始终在任何其他测试套件之前运行的最佳方法是什么?

In the end I refactored my code to permit the core module to be torn down without affecting node-webgl and using a before block to initialize it, like so: 最后,我重构了我的代码以允许核心模块被拆除而不影响node-webgl并使用before块来初始化它,如下所示:

// Run this before all tests to ensure node-webgl is initialized
before(function () {
    if (!global._document) {
        global._document = WebGL.document();
        global._document.setTitle('Machination Graphics Test Suite');
    }
    if (!global._canvas) {
        global._canvas = global._document.createElement('canvas', 640, 480);
    }
});

describe('test suite goes here', function () {
    // Ensure core is ready for us before testing (except when testing core)
    before(function () {
        if (!core.isInitialized()) {
            core.init(global._canvas);
        }
    });
    // Tear down core after all tests are run
    after(function () {
        core.deinit();
    });
    ...
});

Use before() as described in their documentation. 使用before(),如文档中所述。

describe('hooks', function() {
    before(function() {
        // runs before all tests in this block
    });
    ......
});

the function in before will run first and everything else int he describe after it. 之前的函数将首先运行,然后在其后面描述其他所有内容。

hope this helps. 希望这可以帮助。

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

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