使用 mocha.js 从多个文件中加入测试 [英] joining tests from multiple files with mocha.js
问题描述
我正在尝试将来自多个文件的所有测试合并到一个文件中,如下所示:
describe('Controllers', function() {描述('messages.js',函数(){require('./controllertests/messages').test(options);})描述('users.js',函数(){require('./controllertests/users').test(options);})})
我很确定这不是加入测试的最佳方式,我很难找到如何执行此操作的示例:s
如果您想将多个模块加入您的 describe
层次结构,就像您在问题中所做的那样,您所做的几乎是it,除非您想为 Mocha 编写自定义测试加载器.编写自定义加载器不会比已有的代码更容易或更清晰.
以下是我将如何更改一些内容的示例.本例中的 test
子目录组织为:
top.js
:
function importTest(name, path) {描述(名称,函数(){需要(路径);});}var common = require("./common");描述(顶部",函数(){beforeEach(函数(){console.log("在每次测试之前运行一些东西");});importTest("a", './a/a');importTest("b", './b/b');之后(功能(){console.log("经过所有测试");});});
importTest
函数只是为了展示如何处理重复导入多个模块而无需重新输入整个 describe(... require...
每次都是这样.common
模块旨在保存您需要在测试套件的多个模块中使用的内容.我实际上并没有在 top
中使用它但如果需要,它可以在那里使用.
我会在这里注意到 beforeEach
将在每个使用 it
注册的测试之前运行它的代码,无论它们是否出现在 describe
中在 top
或它们出现在任何导入的模块中.使用 --recursive
,必须将 beforeEach
代码复制到每个模块中,或者您可能在每个模块中都有一个 beforeEach
钩子调用从公共模块导入的函数.
此外,after
钩子将在套件中的所有测试之后运行.这不能用 --recursive
复制.如果你使用 --recursive
并将 after
的代码添加到每个模块中,它将每个模块执行一次,而不是整个 只执行一次> 测试.
使用 --recursive
无法复制所有测试都出现在单个 top
标题下.使用 --recursive
每个文件都可以有 describe("top"
但这将为每个文件创建一个新的 top
标题.
common.js
:
var chai = require("chai");变量选项 = {富:富"};出口.选项=选项;出口.柴=柴;出口.assert = chai.assert;
像这样使用名为 common
的 module 是我在一些测试套件中所做的事情,以避免不得不 require
一堆一遍又一遍地保存全局只读变量或不保持状态的函数.我不想像 thgaskell 的回答那样污染 global
对象,因为即使在您的代码可能正在加载的第三方库中,该对象也是真正的全局和可访问的.这在我的代码中是不可接受的.
a/a.js
:
var common = require("../common");var 选项 = common.options;var assert = common.assert;它(等等",函数(){控制台日志(选项.foo);assert.isTrue(false);});
b/b.js
:
it("blah b", function () {});
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);
})
})
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
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.
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");
});
});
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.
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.
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.
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;
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屋!