Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a cleaner way to write parametric tests #2414

Closed
Peque opened this issue Feb 26, 2020 · 7 comments
Closed

Provide a cleaner way to write parametric tests #2414

Peque opened this issue Feb 26, 2020 · 7 comments

Comments

@Peque
Copy link

Peque commented Feb 26, 2020

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
@Peque Peque added the question label Feb 26, 2020
@novemberborn
Copy link
Member

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
Copy link
Contributor

tymfear commented Mar 1, 2020

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
Copy link
Contributor

cdaringe commented Mar 9, 2020

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

@Peque
Copy link
Author

Peque commented May 24, 2020

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

@novemberborn
Copy link
Member

Hey @Peque, per #2414 (comment) 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
Copy link

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

@novemberborn
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants