Mon*_*key 7 unit-testing mocha.js node.js
我想知道是否有一种方法可以从node.js以编程方式执行mocha测试,以便我可以将单元测试与Cloud 9集成.Cloud 9 IDE有一个很好的功能,无论何时保存javascript文件,它都会查找一个文件相同的名称,以"_test"或"Test"结尾,并使用node.js自动运行它.例如,它在自动运行的文件demo_test.js中包含此代码段.
if (typeof module !== "undefined" && module === require.main) {
require("asyncjs").test.testcase(module.exports).exec()
}
Run Code Online (Sandbox Code Playgroud)
有没有这样的东西可以用来进行摩卡测试?有点像摩卡(这个).run()?
Shw*_*ogg 12
以编程方式运行mocha的要点:
需要摩卡:
var Mocha = require('./'); //The root mocha path (wherever you git cloned
//or if you used npm in node_modules/mocha)
Run Code Online (Sandbox Code Playgroud)
Instatiate调用构造函数:
var mocha = new Mocha();
Run Code Online (Sandbox Code Playgroud)
添加测试文件:
mocha.addFile('test/exampleTest'); // direct mocha to exampleTest.js
Run Code Online (Sandbox Code Playgroud)
运行!:
mocha.run();
Run Code Online (Sandbox Code Playgroud)
添加链接函数以编程方式处理传递和失败的测试.在这种情况下,添加一个回调来打印结果:
var Mocha = require('./'); //The root mocha path
var mocha = new Mocha();
var passed = [];
var failed = [];
mocha.addFile('test/exampleTest'); // direct mocha to exampleTest.js
mocha.run(function(){
console.log(passed.length + ' Tests Passed');
passed.forEach(function(testName){
console.log('Passed:', testName);
});
console.log("\n"+failed.length + ' Tests Failed');
failed.forEach(function(testName){
console.log('Failed:', testName);
});
}).on('fail', function(test){
failed.push(test.title);
}).on('pass', function(test){
passed.push(test.title);
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2377 次 |
| 最近记录: |