avajs / ava

Node.js test runner that lets you develop with confidence 🚀
MIT License
20.74k stars 1.41k forks source link

Provide a cleaner way to write parametric tests #2414

Closed Peque closed 4 years ago

Peque commented 4 years ago

Having a function like:

function sum(a, b) {
    return a + b;
}

I would like to create multiple tests with multiple combinations of a and b.

Currently, I can do so by "brute force":

function macroSum(t, a, b) {
    t.is(sum(a, b), a + b);
}
macroSum.title = (providedTitle = "", a, b) =>
    `${providedTitle}: ${a} + ${b}`

test(macroSum, 0, 0);
test(macroSum, 0, 1);
test(macroSum, 0, 2);
test(macroSum, 1, 0);
test(macroSum, 1, 1);
test(macroSum, 1, 2);
test(macroSum, 2, 0);
test(macroSum, 2, 1);
test(macroSum, 2, 2);

Or perhaps even better by using a for loop:

for (const params of [
  [1, 0],
  [1, 1],
]) {
  test(macroSum, ...params)
}

It would be great to have a simple/clearer way to write this.

In Python, with the pytest framework, I can do:

@pytest.mark.parametrize('a', [0, 1, 2])
@pytest.mark.parametrize('b', [0, 1, 2])
def test_sum(a, b):
    assert sum(a, b) == a + b

I think it is a great example. Note how I can define a list of possible values for each variable and how the name of the variable is assigned to the expected parameter in the test. The combinations are handled by the framework, so I do not need to create a list "by hand" to use in a for loop.

It also allows you to do something like (for other use cases):

@pytest.mark.parametrize('a,b', [[0, 0], [0, 1], [1, 0], [1, 1]])
def test_sum(a, b):
    assert sum(a, b) == a + b
novemberborn commented 4 years ago

The for loop is about the best way you can do this currently.

I suppose something like test.parametrize() could take an array of parameters. I don't think that's worth including in AVA itself, at least at this stage. Perhaps somebody could publish a little package that wraps around AVA to provide this.

Let's leave this open for now for others to chime in.

tymfear commented 4 years ago

But you have macros for that, just create a single macro and re-use it. That will make much more clearer code and dubegability.

cdaringe commented 4 years ago

maybe someone ought write a recipe for that and slap it in the docs. py.test fans would eat that up :)

Peque commented 4 years ago

@novemberborn Has this been fixed? Or a recipe added to the docs? Or is the final resolution a "wont fix"?

novemberborn commented 4 years ago

Hey @Peque, per https://github.com/avajs/ava/issues/2414#issuecomment-593120059 I don't think we'll be adding this to AVA. Always open to link to examples or have a recipe, but IMHO it's not worth keeping this issue open for that.

TomerAberbach commented 3 years ago

I went ahead and published pava, which provides a simple utility for writing parameterized tests in ava!

novemberborn commented 3 years ago

@TomerAberbach nice, you should add it to https://github.com/avajs/awesome-ava!