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

时间:2015-03-14 15:35:19

标签: javascript json node.js mocha

我使用ZombieJS和Chai在mocha中设置了一套测试。测试加载一个网站,检查是否正确预订了各种服务,并向网站访问者显示。

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

如何将JSON报告结果传递给另一个node.js脚本,我可以在其中通过电子邮件发送结果。构建电子邮件并发送它将直接使用nodemailer和下划线模板。

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

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

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

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

这运行正常,但我对如何将JSON报告结果变为以下代码中的变量感到迷茫。

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
});

3 个答案:

答案 0 :(得分:41)

您可以像https://github.com/mochajs/mocha/blob/master/lib/runner.js#L40一样收听赛跑者事件并制作自己的报告。

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');
    });

答案 1 :(得分:3)

我建议使用这里解释的摩卡报告人 https://github.com/mochajs/mocha/wiki/Third-party-reporters

像这样调用mocha

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);
  });
}

答案 2 :(得分:2)

通常人们会使用CI机器人来实现您的目标。但是,关于从JSON记者那里获得结果的直接问题,我不知道是否有更好的方法来实现它,但这是我在阅读mocha源后所做的事情。您必须创建套件,Runner并自己获取记者(从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
});