以编程方式运行Mocha并将结果传递给variables或函数

我使用ZombieJS和Chai在摩卡上设置了一套testing。 testing加载一个网站,并检查各种服务是否正确预订,并显示给网站的访问者。

我的目标是testing将每天运行,然后将结果通过电子邮件发送给我的团队。 testing全部按预期运行,但是我遇到的阻塞如下。

如何将JSON记者结果传递给另一个node.js脚本,我可以通过电子邮件发送结果。 build立电子邮件并发送它将会直接使用nodemailer和下划线模板。

我目前的想法是有两种方法。 使用shell脚本运行mochatesting,并将JSON输出传递给节点脚本,并从命令行参数处理JSON。 就像是…

mocha test/services/homepage.js > node email.js 

另一种方法是从节点脚本中运行testing,并将返回的结果放入一个variables中。 我一直在使用这里的信息来运行节点内的testing。

https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically

这运行正确,但我迷失了如何从下面的代码中获取JSON记者结果变成一个variables。

 var Mocha = require('mocha'), Suite = Mocha.Suite, Runner = Mocha.Runner, Test = Mocha.Test; // First, you need to instantiate a Mocha instance var mocha = new Mocha({ reporter: 'json' }); var suite = new Suite('JSON suite', 'root'); var runner = new Runner(suite); var mochaReporter = new mocha._reporter(runner); mocha.addFile( '/Users/dominic/Git/testing-rig/test/services/homepage.js' ); runner.run(function(failures) { // the json reporter gets a testResults JSON object on end var testResults = mochaReporter.testResults; console.log(testResults); // send your email here }); 

如果任何人有一些指导最好的方法来解决这个问题,或者如果我在这个完全错误的方向去,我会很感激任何的build议。

您可以像https://github.com/mochajs/mocha/blob/master/lib/runner.js#L40中那样监听跑步者事件并构build您自己的报告。

 var Mocha = require('mocha'); var mocha = new Mocha({}); mocha.addFile('/Users/dominic/Git/testing-rig/test/services/homepage.js') mocha.run() .on('test', function(test) { console.log('Test started: '+test.title); }) .on('test end', function(test) { console.log('Test done: '+test.title); }) .on('pass', function(test) { console.log('Test passed'); console.log(test); }) .on('fail', function(test, err) { console.log('Test fail'); console.log(test); console.log(err); }) .on('end', function() { console.log('All done'); }); 

嗯,通常人们会使用CI机器人来实现你正在做的事情。 但是,关于从JSON记者那里得到结果的直接问题,我不知道是否有更好的办法来实现它,但这是我看完摩卡的来源之后做的。 你必须创build套件,跑步者,并得到记者自己(复制从https://github.com/mochajs/mocha/blob/master/test%2Freporters%2Fjson.js ):

 var mocha = new Mocha({ reporter: 'json' }); var suite = new Suite('JSON suite', 'root'); var runner = new Runner(suite); var mochaReporter = new mocha._reporter(runner); runner.run(function(failures) { // the json reporter gets a testResults JSON object on end var testResults = mochaReporter.testResults; // send your email here }); 

我build议使用摩卡记者在这里解释https://github.com/mochajs/mocha/wiki/Third-party-reporters

像这样调用摩卡

 MyReporter = require('./MyReporter'); mocha({ reporter: MyReporter })` 

MyReporter.js文件将如下所示

 var mocha = require('mocha'); module.exports = MyReporter; function MyReporter(runner) { mocha.reporters.Base.call(this, runner); var passes = 0; var failures = 0; runner.on('pass', function(test){ passes++; console.log('pass: %s', test.fullTitle()); }); runner.on('fail', function(test, err){ failures++; console.log('fail: %s -- error: %s', test.fullTitle(), err.message); }); runner.on('end', function(){ console.log('end: %d/%d', passes, passes + failures); process.exit(failures); }); }