How to run setup method once in a group of test classes in junit4
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, October 21, 2008 10:20 PM

I have multiple test classes e.g. TestA.class, TestB.class, TestC.class..., which have the SAME @BeforeClass method. To make the test run efficient, I would like to only run the @BeforeClass method ONCE for ALL these test classes. I tried to put all these classes in junit4's default @RunWith feature, the sample code snippet is: @RunWith(Suite.class) @SuiteClasses( { TestA.class, TestB.class, TestC.class } ) public class TrySuite { @BeforeClass public static void doBeforeTestClassRun(){ //do stuff } } But there are two problems in this approach: 1. I don't want    More>>

I have multiple test classes e.g. TestA.class, TestB.class, TestC.class..., which have the SAME @BeforeClass method. To make the test run efficient, I would like to only run the @BeforeClass method ONCE for ALL these test classes.

I tried to put all these classes in junit4's default @RunWith feature, the sample code snippet is:

			

@RunWith(Suite.class)
@SuiteClasses(
{
TestA.class,
TestB.class,
TestC.class
}

)

public class TrySuite
{
@BeforeClass
public static void doBeforeTestClassRun(){
//do stuff
}

}

But there are two problems in this approach:

1. I don't want to manually add test classes to @SuiteClasses(), which is a growing list, and hard to maintain.


2. The ant report for this test only shows the information for the suite class --in my case, TrySuite, and don't display which individual test class fail, which cause debug inconvenience.

e.g.
[junit] Running TrySuite
[junit] Tests run: 25, Failures: 1, Errors: 2, Time elapsed: 2 sec


Any suggestions are highly appreciated!

   <<Less
About | Sitemap | Contact