caolan / nodeunit

Easy unit testing in node.js and the browser, based on the assert module.
MIT License
1.9k stars 370 forks source link

Single test run with multiple data #221

Open Strix-CZ opened 11 years ago

Strix-CZ commented 11 years ago

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.

alexmipego commented 9 years ago

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.

rquadling commented 6 years ago

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.