Unit testing 使用cloud9的mocha测试,从node.js执行mocha测试
我想知道是否有一种方法可以从node.js以编程方式执行mocha测试,这样我就可以将单元测试与Cloud 9集成。Cloud9IDE有一个很好的特性,每当保存javascript文件时,它都会查找同名文件,以“_test”或“test”结尾,并使用node.js自动运行该文件。例如,它在自动运行的demo_test.js文件中包含此代码段Unit testing 使用cloud9的mocha测试,从node.js执行mocha测试,unit-testing,node.js,mocha.js,Unit Testing,Node.js,Mocha.js,我想知道是否有一种方法可以从node.js以编程方式执行mocha测试,这样我就可以将单元测试与Cloud 9集成。Cloud9IDE有一个很好的特性,每当保存javascript文件时,它都会查找同名文件,以“_test”或“test”结尾,并使用node.js自动运行该文件。例如,它在自动运行的demo_test.js文件中包含此代码段 if (typeof module !== "undefined" && module === require.main) { re
if (typeof module !== "undefined" && module === require.main) {
require("asyncjs").test.testcase(module.exports).exec()
}
有这样的东西我可以用来做摩卡咖啡测试吗?类似于mocha(this.run()?以编程方式运行mocha的要点: 需要摩卡咖啡:
var Mocha = require('./'); //The root mocha path (wherever you git cloned
//or if you used npm in node_modules/mocha)
Instatitate调用构造函数:
var mocha = new Mocha();
添加测试文件:
mocha.addFile('test/exampleTest'); // direct mocha to exampleTest.js
运行它!:
mocha.run();
添加链接函数以编程方式处理通过和失败的测试。在这种情况下,添加回调以打印结果:
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);
});
您的里程数可能会有所不同,但我不久前调制了以下一种班轮,它对我非常有用:
if (!module.parent)(new(require("mocha"))()).ui("exports").reporter("spec").addFile(__filename).run(process.exit);
此外,如果您希望它以Cloud9所期望的asyncjs
格式输出,则需要提供一个特殊的报告器。下面是一个非常简单的例子,说明一个简单的记者应该是什么样子:
if (!module.parent){
(new(require("mocha"))()).ui("exports").reporter(function(r){
var i = 1, n = r.grepTotal(r.suite);
r.on("fail", function(t){ console.log("\x1b[31m[%d/%d] %s FAIL\x1b[0m", i++, n, t.fullTitle()); });
r.on("pass", function(t){ console.log("\x1b[32m[%d/%d] %s OK\x1b[0m", i++, n, t.fullTitle()); });
r.on("pending", function(t){ console.log("\x1b[33m[%d/%d] %s SKIP\x1b[0m", i++, n, t.fullTitle()); });
}).addFile(__filename).run(process.exit);
}