If the built-in reporters don't do quite what you need, you can write your own. This document describes how to write and configure your own custom test reporter.
Your reporter should have total
and pass
properties, and implement report(prefix, data)
and
finish()
methods.
The report
method gets called for each test with prefix
and data
arguments:
prefix
- name of the test runner (ie PhantomJS)data
passed
- whether test passedfailed
- whether test failedpending
- whether test is still pendingname
- test nameerror
- Error object (passed
,message
,stack
)error.message
- error stacklogs
- test output (array of log objects (type
("log", "warn", "error", "info") andtext
))skipped
- skipped test countrunDuration
- how long the test ranlauncherId
- Unique ID of the launcheritems
- Array of error objects
The finish
method gets called when tests are complete, and can output summary information.
In addition, an optional reportMetadata(tag, metadata)
method gets called whenever an adapter
emits a test-result-metadata
event, allowing customized processing on metadata generated by your
tests. To find out more, check out
an example.
The constructor should take an (optional) output stream and initialize properties:
function MyReporter(out) {
this.out = out || process.stdout;
this.total = 0;
this.pass = 0;
}
The report
method should increment counters and output results as needed; the finish
method should output summary information:
MyReporter.prototype = {
report: function(prefix, data) {
// increment counters
this.total++;
if (data.passed) {
this.pass++;
}
// output results
var status = data.passed ? 'ok' : 'failed';
this.out.write(prefix+'\t'+status+'\t'+data.name.trim()+'\n');
},
finish: function() {
this.out.write(this.passed+'/'+this.total+' tests passed\n')
}
}
To use your custom reporter, set reporter
in your testem.js
config file:
var MyReporter = require('./my-reporter');
module.exports = {
"framework": "mocha",
"src_files": [
"src/*.js",
"tests/*_tests.js"
]
"reporter": MyReporter
};