Javascript 如何在多个文件中设置 mocha 测试用例的执行顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28229424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set execution order of mocha test cases in multiple files
提问by tnishada
I have two javascript files which contain mocha test cases.
我有两个包含 mocha 测试用例的 javascript 文件。
//----------abc.js -------------
describe("abc file", function(){
it("test 1" , function(){
assert.equal(20 , 20);
});
});
//---------xyz.js--------------
describe("xyz file", function(){
it("test 1" , function(){
assert.equal(10 , 10);
});
});
I have put them in a folder called testand when I execute the mochacommand the first file(abc.js) is always executed before xyz.js.
I thought this might be due to alphabetical ordering and renamed the files as
我将它们放在一个名为的文件夹中test,当我执行mocha命令时,第一个文件(abc.js)总是在 xyz.js 之前执行。我认为这可能是由于按字母顺序排列并将文件重命名为
abc.js => xyz.js
xyz.js => abc.js
but still, the content of the xyz.js (previously abc.js) is executed first. How can I change the execution order of these test files?
但仍然首先执行 xyz.js(以前是 abc.js)的内容。如何更改这些测试文件的执行顺序?
采纳答案by Louis
Mocha has a --sort(short -S) option that sorts test files:
Mocha 有一个--sort(short -S) 选项可以对测试文件进行排序:
$ mocha --help
[...]
-S, --sort sort test files
[...]
回答by Peter Lyons
In the second file, require the first one:
在第二个文件中,需要第一个:
--- two.js ---
require("./one")
Mocha will run the tests in the order the describecalls execute.
Mocha 将按照describe调用执行的顺序运行测试。
回答by Priyanshu Jindal
I follow a totally seperate solution for this.
我遵循一个完全独立的解决方案。
Put all your tests in a folder named test/ and Create a file tests.js in the root directory in the order of execution
将所有的测试放在一个名为 test/ 的文件夹中,并按照执行顺序在根目录下创建一个文件 tests.js
--- tests.js ---
require('./test/one.js')
require('./test/two.js')
require('./test/three.js')
And in the tests files one.js, two.js and so on write your simple mocha tests
在测试文件 one.js、two.js 等中编写简单的 mocha 测试
this way if you want to run them in the order you have defined then just run mocha tests.js
这样,如果您想按照您定义的顺序运行它们,那么只需运行 mocha tests.js
回答by djfm
Since mocha sorts files in alphabetical order, I usually prefix my test files names with numbers, like:
由于 mocha 按字母顺序对文件进行排序,我通常在我的测试文件名前加上数字,例如:
0 - util.js1 - something low level.js2 - something more interesting.js
0 - util.js1 - something low level.js2 - something more interesting.js
etc.
等等。
In addition to being really easy to maintain (no gulp grunt or any of that nonsense, no editing your package.json...), it provides the benefit that:
除了非常容易维护(没有咕噜声或任何废话,没有编辑你的 package.json ......),它还提供了以下好处:
- people reading your source code get an idea of the structure of your program, starting from the less interesting parts and moving up to the business layer
- when a test fails, you have some indication of causality (if something failed in
1 - something.jsbut there are no failures in0 - base.jsthen it's probably the fault of the layer covered by1 - something.js
- 阅读你的源代码的人会了解你的程序结构,从不太有趣的部分开始,向上移动到业务层
- 当测试失败时,你有一些因果关系的迹象(如果某事失败
1 - something.js但没有失败,0 - base.js那么它可能是被覆盖的层的错误1 - something.js
If you're doing real unit tests of course order should not matter, but I'm rarely able to go with unit tests all the way.
如果您正在进行真正的单元测试,当然顺序应该无关紧要,但我很少能够一路进行单元测试。
回答by David Schneider
If you prefer a particular order, you can list the files (in order) as command-line arguments to mocha, e.g.:
如果您更喜欢特定的顺序,您可以将文件(按顺序)列为 的命令行参数mocha,例如:
$ mocha test/test-file-1.js test/test-file-2.js
$ mocha test/test-file-1.js test/test-file-2.js
To avoid a lot of typing every time you want to run it, you could turn this into an npmscript in your package.json:
为避免每次要运行时进行大量输入,您可以将其转换为npm您的脚本package.json:
{
// ...
"scripts": {
"test": "mocha test/test-file-1.js test/test-file-2.js"
}
// ...
}
Then run your suite from the command line:
然后从命令行运行您的套件:
$ npm test
Or if you're using Gulp, you could create a task in your gulpfile.js:
或者,如果您使用 Gulp,您可以在您的gulpfile.js:
var gulp = require('gulp');
var mocha = require("gulp-mocha");
gulp.task("test", function() {
return gulp.src([
"./test/test-file-1.js",
"./test/test-file-2.js"
])
.pipe(mocha());
});
Then run $ gulp test.
然后运行$ gulp test。
回答by André Marcelino
The way it worked for my tests to be executed in a specific order was to create a separate test.js file and then added a describefor each mocha test file I'd wanted to execute.
我的测试以特定顺序执行的工作方式是创建一个单独的 test.js 文件,然后describe为我想要执行的每个 mocha 测试文件添加一个。
test.js:
测试.js:
describe('test file 1', function() {
require('./test1.js')
})
describe('test file 2', function() {
require('./test2.js')
})
Then simply run mocha test.js
然后简单地运行 mocha test.js
回答by user12342217
I am exporting an array with all required files and that is the way I tell mocha the order of execution through index.js file in the folder with all my test files:
我正在导出一个包含所有必需文件的数组,这就是我通过包含所有测试文件的文件夹中的 index.js 文件告诉 mocha 执行顺序的方式:
const Login = require('../login');
const ChangeBudgetUnit = require('./changeBudgetUnit');
const AddItemsInCart = require('./addItemsInCart');
// if the order matters should export array, not object
module.exports = [
Login,
ChangeBudgetUnit,
AddItemsInCart
];

