Coming from Python, I am used to this syntax when writing parametric tests (hope it is self-explanatory):
@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
The testing framework will then run test_sum()
for each combination of a
and b
, considering each combination as a different test (i.e.: the 9 combinations can fail/pass independently).
I have been playing around with Ava and, using macros, I can easily run the same test with different parameters:
import test from "ava";
function sum(a, b) {
return a + b;
}
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);
The only problem is that I need to call test()
multiple times explicitly. Is there any way I could do what I am used to do in Python? (declare a list of values for each parameter and let the framework combine them and run all the combinations for me)
Note it is not the same as embedding a loop inside a test. With parametric tests each combination is a separate test to the framework, with a different title/identifier and which can fail/pass independently of the other parameter combinations.
That means, what I am looking for is not a single test like:
test('all combinations', t => {
[0, 1, 2].forEach(a => {
[0, 1, 2].forEach(b => {
t.is(sum(a, b), a + b);
});
});
});
Aucun commentaire:
Enregistrer un commentaire