Jodes Jodes - 3 months ago 14
Javascript Question

Equivalent to @dataProvider annotation for unit testing Javascript

PHPUnit has a useful feature for testing code with a data set of inputs and expected outputs with the @dataProvider test method annotation.

I'm using JS-Test-Driver to write unit tests for javascript. Am I correct in saying it has no similar feature? How should bulk input/output data be tested in Javascript?

Answer

The answer is to use the Parameterize plugin.

The test would then look like this:

QUnit
    .cases([
        { a : 2, b : 2, expectedSum : 4 },
        { a : 5, b : 5, expectedSum : 10 },
        { a : 40, b : 2, expectedSum : 42 }
    ])
    .test("Sum test", function(params) {
        var actualSum = sum(params.a, params.b);
        equal(actualSum, params.expectedSum);
    });
Comments