busterjs / buster

Abandoned - A powerful suite of automated test tools for JavaScript.
http://docs.busterjs.org
Other
448 stars 37 forks source link

Add data provider capability #153

Open jgornick opened 12 years ago

jgornick commented 12 years ago

PHPUnit offers something called a data provider which allows a single test to be run with different datasets. Documentation can be found @ http://www.phpunit.de/manual/3.6/en/writing-tests-for-phpunit.html#writing-tests-for-phpunit.data-providers.

I've come up with a basic example that allows a user to specify a test with a set of data (https://gist.github.com/c91cc8ce925c6d9b4fe9). The only issue I have here is I don't know how you would be able to intercept an assertion failure to produce a proper failure message that follows a similar pattern found in PHPUnit (Failure: "test adding" with data set #3 (1, 1, 3))

cjohansen commented 12 years ago

This will be very useful, thanks!

augustl commented 12 years ago

I think the only thing we need to make doing things like this super easy, is to allow text contexts to be arrays.

"adding": [
    function () { assert(true) },
    function () { assert(false) }
]

If we allow this, performing the same test on different data is as simple as:

"adding": [
    [0, 0, 0],
    [0, 1, 1],
    [1, 0, 1],
    [1, 1, 3]
].map(function (data) {
    return function () {
        assert.equals(data[0], data[1] + data[2])
    }
})
jgornick commented 12 years ago

@augustl Interesting approach. It's a little minimalistic for me, but I can get behind it.

What about custom failure messages? Would it still be possible with this approach?

sveisvei commented 12 years ago

Hopefully this will 'provide' me a better way to write this: https://github.com/sveisvei/FINN-WebAds/blob/master/test/size-cases-test.js

cjohansen commented 12 years ago

@augustl providing a thin abstraction for this will allow us to give better error messages, and I think the test case ends up slightly more self-explaining. We also relieve the user of a little bit of logic, which is never a bad idea in tests.

cjohansen commented 12 years ago

@sveisvei that looks exactly like the kind of test this would be useful for.