Background
I have just been fooling around with Junit's parameterization support. Here is what I learned so far
JUnit 4 has support for parameterization. In short, write test points, and run them for different parameters.
for eg.
The Test point could be
test() { launch browser for (browsername); }
and it needs to be run for multiple browsers.
The Parameter here is the 'browsername'.
JUnit allows you to define a @Parameters decorated method (with any name) which you use to define what the list of browser names are. Of course, you can even have multiple parameters (which is why the weird List of array syntax appears)
Thus, if you want to run launchbrowser() with 'ie', 'firefox', 'chrome', you write (Note: you need to add @RunWith(Parameterized.class). See Junit4 doc for details.
Now what?
I would next like to take this a step further. I want a base class that contains my parameterization. I want my actual test classes to not worry about that, and simply write tests that will be automatically parameterized. Classic example, a file i/o api, that needs to be tested for a text file, binary file, remote file, local file etc
I can have several classes defined to hold my parameterization, then at authoring time, I pick one to inherit from, depending on what I want to parameterize my tests on. Alternatively, you can also define multiple parameters.
One approach is:
TestA,TestB <--- BaseClassA
TestC <--- BaseClassB
Another is TestA, TestB, TestC <-- BaseClassWithMultipleParameters
Approach 2 has advantage of code reuse, but this may not be what you always want.
What does this achieve?
My Test Classes simply extend from BaseClassA or B, but don't have any parameterization code. I can then create a no. of subclasses that all get the parameterization for free! The consumer test classes don't even have to explicitly know their tests are being parameterized!
I am just beginning to give this some thought. Perhaps there are better ways to do this.
I would mainly like to get rid of the inheritance model.
More on this later...
I have just been fooling around with Junit's parameterization support. Here is what I learned so far
JUnit 4 has support for parameterization. In short, write test points, and run them for different parameters.
for eg.
The Test point could be
test() { launch browser for (browsername); }
and it needs to be run for multiple browsers.
The Parameter here is the 'browsername'.
JUnit allows you to define a @Parameters decorated method (with any name) which you use to define what the list of browser names are. Of course, you can even have multiple parameters (which is why the weird List of array syntax appears)
Thus, if you want to run launchbrowser() with 'ie', 'firefox', 'chrome', you write (Note: you need to add @RunWith(Parameterized.class). See Junit4 doc for details.
@RunWith(Parameterized.class)
public class myTest {
@Parameters
public static Collection
Now what?
I would next like to take this a step further. I want a base class that contains my parameterization. I want my actual test classes to not worry about that, and simply write tests that will be automatically parameterized. Classic example, a file i/o api, that needs to be tested for a text file, binary file, remote file, local file etc
I can have several classes defined to hold my parameterization, then at authoring time, I pick one to inherit from, depending on what I want to parameterize my tests on. Alternatively, you can also define multiple parameters.
One approach is:
TestA,TestB <--- BaseClassA
TestC <--- BaseClassB
Another is TestA, TestB, TestC <-- BaseClassWithMultipleParameters
Approach 2 has advantage of code reuse, but this may not be what you always want.
What does this achieve?
My Test Classes simply extend from BaseClassA or B, but don't have any parameterization code. I can then create a no. of subclasses that all get the parameterization for free! The consumer test classes don't even have to explicitly know their tests are being parameterized!
I am just beginning to give this some thought. Perhaps there are better ways to do this.
I would mainly like to get rid of the inheritance model.
More on this later...
Comments
Post a Comment