I'm using mocha and trying to build a testing system which reports tests individually. The goal is to have traceability between tests defined in the requirements for the project and unit tests. So, for example, the test 'Must be able to create new widgets' is in the requirements database with id of '43', I want the unit test which tests that criteria to report something like Test 43, Must be able to create new widgets, pass
, and then update the corresponding db entry (another service could be responsible for this).
Can this be done in mocha? The only thing I've found so far is to replace text in the it()
function with the test id, and use the json reporter to process the results afterwards (but then I don't get the text for what is being tested, unless I combine them and do some kind of parsing). Note: not all tests would have an id.
Here's an example of the kind of functionality I'm hoping for
describe("Widget" function() {
it("should allow creation of widgets", function() {
this.id = 43;
result = widget.create();
expect.result.to.exist;
});
});
And then either a hook, like
afterEach(function(test) {
if (test.hasOwnProperty('id')) {
report(test.result);
}
});
Or a custom reporter, or an adapter of some sort.
runner.on('test end', function(test) {
console.log(test.id); //doesn't exist, but i want it to
report(test);
});
This depends on your assertion library. With Chai, you havethe optional field for text.
assert.should.exist(result, 'expect Result to exist (Id 43)');
With Jasmine, you can add the test reference to your it():
describe("Widget" function() {
it("should allow creation of widgets (Id 43)", function() {
To use the Mocha custom reporters you could try to define one in your test suite.
module.exports = MyReporter;
function MyReporter(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);
});
}
There are really 2 suggestions here. The first is the simplest, and is to simply add your id to the description of the it() and then that will show you what has passed and failed. That would be the quickest way to reach your goal.
However, if you wanted to have the fancier method, and could test to ensure things are set, then you could use the custom reporter, which would allow you to fail a test if the ID was not set.
I have the following jquery selector which I am trying to convert to regular javascript. $("#lelement>*").on("dblclick", function(){ }); what would the equivalent be with regular javascript ? Can ...
I have the following jquery selector which I am trying to convert to regular javascript. $("#lelement>*").on("dblclick", function(){ }); what would the equivalent be with regular javascript ? Can ...
I have 5 boxes and an array of items. How do I display 5 items in 5 boxes, randomly selected without repetition? Is it a problem with: ptags[index].textContent = item[0].label; that it is not ...
I have 5 boxes and an array of items. How do I display 5 items in 5 boxes, randomly selected without repetition? Is it a problem with: ptags[index].textContent = item[0].label; that it is not ...
Often in the interview questions it is heard that Why one should use redux form in the ReactJs instead of simple form. which extra ordinary facilities it provides to the developer ? Then what should ...
Often in the interview questions it is heard that Why one should use redux form in the ReactJs instead of simple form. which extra ordinary facilities it provides to the developer ? Then what should ...
I am trying to use phantomjs to automate the process of login into facebook. The issue is that facebook uses js codes to set cookies before the login form is submitted. Clicking on the email input box ...
I am trying to use phantomjs to automate the process of login into facebook. The issue is that facebook uses js codes to set cookies before the login form is submitted. Clicking on the email input box ...