coi*_*iso 78 unit-testing mocha.js node.js
我正在尝试将多个文件中的所有测试加入到一个文件中,如下所示:
describe('Controllers', function() {
describe('messages.js', function() {
require('./controllertests/messages').test(options);
})
describe('users.js', function() {
require('./controllertests/users').test(options);
})
})
Run Code Online (Sandbox Code Playgroud)
我很确定这不是加入测试的最佳方式,我有一些难以找到如何做到这一点的例子:s
Lou*_*uis 99
如果你想包含多个模块到您的describe
层次结构就像你在你的问题做什么,你在做什么是相当多的它,除非你想要写摩卡自定义测试装载机.编写自定义加载器并不容易,或者使代码比现有代码更清晰.
这是一个如何改变一些事情的例子.test
此示例中的子目录组织为:
.
??? test
??? a
? ??? a.js
??? b
? ??? b.js
??? common.js
??? top.js
Run Code Online (Sandbox Code Playgroud)
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");
});
});
Run Code Online (Sandbox Code Playgroud)
该importTest
功能只是为了显示它如何能够处理导入多个模块的重复而不必重新输入整个describe(... require...
事情每一次.该common
模块旨在保存您需要在测试套件的多个模块中使用的内容.我实际上并没有使用它,top
但如果需要,它可以在那里使用.
我将在这里指出,beforeEach
将分别与登记的每一个测试之前运行的代码it
是否出现在内部describe
的top
或出现在任何导入模块.有了--recursive
,beforeEach
代码必须被复制到每个模块中,或者你可能beforeEach
在每个模块中都有一个钩子,它调用从公共模块导入的函数.
此外,after
挂钩将在套件中的所有测试之后运行.这不能复制--recursive
.如果您使用--recursive
并添加after
每个模块的代码,则每个模块将执行一次,而不是仅对整个测试执行一次.
所有测试都出现在单个top
标题下,无法通过使用来复制--recursive
.随着--recursive
每个文件可以有describe("top"
,但是这将创建一个新的top
为每个文件标题.
common.js
:
var chai = require("chai");
var options = {
foo: "foo"
};
exports.options = options;
exports.chai = chai;
exports.assert = chai.assert;
Run Code Online (Sandbox Code Playgroud)
使用一个名为this 的模块common
是我在我的一些测试套件中所做的事情,以避免require
一遍又一遍地拥有一堆东西并保存全局只读变量或不保持状态的函数.我不global
喜欢在thgaskell的答案中污染对象,因为这个对象真正是全局的,即使在你的代码可能正在加载的第三方库中也是可访问的.这不是我在代码中可以接受的东西.
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);
});
Run Code Online (Sandbox Code Playgroud)
b/b.js
:
it("blah b", function () {});
Run Code Online (Sandbox Code Playgroud)
Ian*_*son 24
虽然这可能与问题没有直接联系,但我一直在寻找的答案是:
$ mocha --recursive
Run Code Online (Sandbox Code Playgroud)
将在"test"文件夹的子目录中执行所有测试.整齐.保存必须维护我想要加载的测试列表,实际上只是始终运行所有内容.
thg*_*ell 14
没有什么可以阻止您运行多个测试文件.通常,每个测试不应该依赖于另一个测试的结果,因此共享变量不是您想要做的事情.
这是一个如何组织测试文件的示例.
.
??? app.js
??? test
??? common.js
??? mocha.opts
?
??? controllers
? ??? messages-controller.js
? ??? users-controller.js
?
??? modles
??? messages-model.js
??? users-model.js
Run Code Online (Sandbox Code Playgroud)
然后在mocha.opts
文件内部,确保设置--recursive
选项.
mocha.opts
--ui bdd
--recursive
Run Code Online (Sandbox Code Playgroud)
如果是要在所有文件,包括通用模块,您可以添加到common.js
文件中.test
目录根目录下的文件将在嵌套目录中的文件之前运行.
common.js
global.chai = require('chai');
global.assert = chai.assert;
global.expect = chai.expect;
chai.should();
chai.config.includeStack = true;
process.env.NODE_ENV = 'test';
// Include common modules from your application that will be used among multiple test suites.
global.myModule = require('../app/myModule');
Run Code Online (Sandbox Code Playgroud)
I know this is an old post but I wanted to chime in with what has been a good solution to me, very similar to the method proposed by OP.
The project I'm working on is well tested and the tests keep growing. I ended up using require
because it is synchronous and therefore makes it a bit easier to compose your tests without too much change in architecture:
// inside test/index.js
describe('V1 ROUTES', () => {
require('./controllers/claims.test');
require('./controllers/claimDocuments.test');
require('./controllers/claimPhotos.test');
require('./controllers/inspections.test');
require('./controllers/inspectionPhotos.test');
require('./controllers/versions.test');
require('./services/login.v1.test');
});
describe('V2 ROUTES', () => {
require('./services/login.v2.test');
require('./services/dec-image.v2.test');
});
describe('V3 ROUTES', () => {
require('./services/login.v3.test');
require('./services/getInspectionPhotosv3.test');
require('./services/getPolicyInfo.v3.test');
});
describe('ACTIONS', () => {
require('./actions/notifications.test');
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
43848 次 |
最近记录: |