Parameterized Tests, a proposal
Hi,
I just got around to looking into parameterized tests a little more.
But it confirmed the impression I got when they came out with JUnit 4.
They are sooo clunky, that I rather write a test that calls a
helper-method with parameters instead.
But this time I had time to ask myself: Why is it so clunky?
In what way could it be better.
So I came up with an idea, how I would like it to be.
And here is an example:
@RunWith(Parameterized.class)
public class MyParameterizedTest {
private Object[] data = new Object[][] {
{"param1", 2, 3},
{"other", 5, 99},
{"more", 234, 5875727343}
};
// The parameter given as "data" here, specifies that
// attribute, that contains the parameters for the test
@ParameterizedTest("data")
public void testWhatever(String str, int i, long j) {
// This method gets called 3 times
// once for each line in the above Object[]
// do whatever here
}
@Test
public void plainNonParameterizedTest() {
// this is a normal unit test that is
// called just once
}
}
I think it's a lot easier this way.
It allows mixing of parameterized tests and non parameterized tests, if
you like.
I allows for several parameterized tests in a single class.
And I don't see disadvantages.
It could be even simpler, if it were incorporated into the existing
JUnit Test-annotation, like so:
// no @RunWith-Annotation here
public class MyParameterizedTest {
private Object[] data = new Object[][] {
{"param1", 2, 3},
{"other", 5, 99},
{"more", 234, 5875727343}
};
// The parameter given as "data" here, specifies that
// attribute, that contains the parameters for the test
// Use of standard Test-annotation with a new parameter
@Test(parameterized="data")
public void testWhatever(String str, int i, long j) {
// This method gets called 3 times
// once for each line in the above Object[]
// do whatever here
}
}
What do you think?
I already tried to implement the first version, since it's non
intrusive, but looking into extending JUnit, I found that it's a pain in
the behind, if you don't want to extends internals-stuff.
But maybe I'm just misunderstanding or missing things.
But that's another story for another post... shortly...
Greetings,
Malte