使用mocha.js加入来自多个文件的测试 [英] joining tests from multiple files with mocha.js
问题描述
我正在尝试将多个文件中的所有测试合并到一个文件中,如下所示:
I'm trying to join all the tests from multiple files in one file, something like this:
describe('Controllers', function() {
describe('messages.js', function() {
require('./controllertests/messages').test(options);
})
describe('users.js', function() {
require('./controllertests/users').test(options);
})
})
我很确定这不是参加测试的最佳方法,我很难理解如何做到这一点:s
I'm pretty sure this is not the best way to join tests, I'm having some dificulty finding examples of how to do this :s
推荐答案
如果您想像在问题中一样将多个模块纳入您的describe
层次结构中,那么您正在做的是除非您想为Mocha编写自定义测试加载器,否则它几乎是 it .编写自定义加载程序不会比现在更容易,或者使您的代码更清晰.
If you want to include multiple modules into your describe
hierarchy like you are doing in your question, what you are doing is pretty much it, unless you want to write a custom test loader for Mocha. Writing the custom loader would not be easier or make your code clearer than what you already have.
这是我如何改变一些事情的一个例子.在本示例中,test
子目录的组织方式为:
Here's an example of how I would change a few things. The test
subdirectory in this example is organized as:
.
└── test
├── a
│ └── a.js
├── b
│ └── b.js
├── common.js
└── top.js
top.js
:
function importTest(name, path) {
describe(name, function () {
require(path);
});
}
var common = require("./common");
describe("top", function () {
beforeEach(function () {
console.log("running something before each test");
});
importTest("a", './a/a');
importTest("b", './b/b');
after(function () {
console.log("after all tests");
});
});
importTest
函数只是为了展示如何处理重复导入多个模块而不必每次都重新键入整个describe(... require...
事物的可能性. common
模块旨在容纳您需要在测试套件的多个模块中使用的模块.我实际上并没有在top
中使用它,但是如果需要的话,可以在其中使用它.
The importTest
function is just to show how it would be possible to handle the repetition of importing multiple modules without having to retype the whole describe(... require...
thing every single time. The common
module is meant to hold what you need to use in multiple modules of the test suite. I'm not actually using it in top
but it could be used there, if needed.
我会在这里指出,beforeEach
将在每次向it
注册的每个测试之前运行其代码,无论它们出现在top
中的describe
内部还是它们出现在任何模块中导入的.使用--recursive
,必须将beforeEach
代码复制到每个模块中,或者在每个模块中可能都有一个beforeEach
钩子,以调用从公共模块导入的函数.
I will note here that the beforeEach
will run its code before each and every single test registered with it
whether they appear inside the describe
in top
or they appear in any of the modules imported. With --recursive
, the beforeEach
code would have to be copied into each module or perhaps you'd have a beforeEach
hook in each module that calls a function imported from a common module.
此外,after
挂钩将在套件中的所有测试之后运行.这不能用--recursive
复制.如果您使用--recursive
并将after
的代码添加到每个模块,则它将对每个模块执行一次,而不是针对 whole 测试仅执行一次.
Also, the after
hook will run after all tests in the suite. This cannot be replicated with --recursive
. If you use --recursive
and add the code of after
to each module, it will be executed once per module rather than just once for the whole test.
所有测试都显示在单个top
标题下,无法使用--recursive
复制.使用--recursive
,每个文件都可以具有describe("top"
,但这将为每个文件创建一个新的top
标题.
Having all tests appear under a single top
heading cannot be replicated by using --recursive
. With --recursive
each file could have describe("top"
but this would create a new top
heading for each file.
common.js
:
var chai = require("chai");
var options = {
foo: "foo"
};
exports.options = options;
exports.chai = chai;
exports.assert = chai.assert;
像这样使用名为common
的模块是我在一些测试套件中所做的事情,以避免一遍又一遍地require
一堆东西并保存全局<不保持状态的strong>只读变量或函数.我不想像thgaskell的回答那样污染global
对象,因为该对象是真正的全局对象,即使在您的代码可能正在加载的第三方库中也可以访问.在我的代码中,这不是我可以接受的.
Using a module named common
like this is something I've done in some of my test suites to avoid having to require
a bunch of stuff over and over and to hold global read-only variables or functions that don't keep state. I prefer not to pollute the global
object like in thgaskell's answer because this object is truly global and accessible even in third party libraries your code may be loading. This is not something I find acceptable in my code.
a/a.js
:
var common = require("../common");
var options = common.options;
var assert = common.assert;
it("blah a", function () {
console.log(options.foo);
assert.isTrue(false);
});
b/b.js
:
it("blah b", function () {});
这篇关于使用mocha.js加入来自多个文件的测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!