nodeunit
nodeunit copied to clipboard
Single test run with multiple data
Hello, I have found TestNG's ability to run single test case with multiple data to be very useful. In TestNG it's called DataProvider. I have implemented similar functionality with this global function:
global.testMultiple = function (data, testFunction) {
var suite = {};
var length = data.length;
for (var i=0; i<length; ++i) {
suite['#' + i] = (function(index){
return function(test) {
testFunction(test, data[index], index);
};
})(i);
}
return suite;
};
You can use this function in this way:
exports.addition = testMultiple(
[
{x: 1, y: 1, result: 2},
{x: 2, y: 2, result: 4},
{x: 3, y: 2, result: 6},
],
function(test, data) {
test.equal(data.x + data.y, data.result);
test.done();
}
);
It works fine, but I am wondering if it is possible to get rid of the global function and integrate this functionality directly in nodeunit in some smart way.
I've found this need today too, although not exactly as you describe. I have a suite that tests a rest service, but I need to run the suite on multiple different endpoints. The main difference from what the reporter suggests is that I also need the setups/teardowns and nested groups to be correctly processed.
FYI. I've added a PR to https://github.com/Twipped/nodeunit-dataprovider/pull/2 to allow for named sets of data (similar to how you can in PHPUnit). Makes things a lot easier to see when set number 523 is having an issue.