Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Junit4: Running a Suite of particular Test methods

Is there a way to create a suite of test methods, not just test classes?

I'd like to put together a test suite that just runs particular tests methods from a test class. I don't see a way to do this from my limited junit knowledge and from searching the web.

like image 463
Aaron Avatar asked Jan 24 '11 14:01

Aaron


1 Answers

Use Categories feature in JUnit4.

Example: if some methods scattered both in ATest and BTest are expected to executed :

//Define Categories
@RunWith(Categories.class)  
@IncludeCategory(NeedTest.class)  
@SuiteClasses({ ATest.class,  BTest.class })  
class MySuite{
...
}

Then in ATest and BTest, annotate your expect methods as:

@Test
@Category(NeedTest.class)  
public void test()

When you run MySuite, only the methods annotated with @Category(NeedTest.class) will be executed. Of course, you could create multiple test categories,

ps: NeedTest.class is just a marker class, it can be any class.

like image 102
卢声远 Shengyuan Lu Avatar answered Sep 19 '22 12:09

卢声远 Shengyuan Lu