6

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.

djangofan
  • 28,471
  • 61
  • 196
  • 289
Aaron
  • 3,249
  • 4
  • 35
  • 51
  • Just some little stupid thought: Create new class and paste important methods you want to run from various tests, it's complete fake but it should work for you ... – Xorty Jan 24 '11 at 19:10

1 Answers1

8

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.

卢声远 Shengyuan Lu
  • 31,208
  • 22
  • 85
  • 130
  • Hi Shengyuan, if we use our customize runner, then this feature will be disabled for us except we implement this feature at our own runner, right? – Tim Feb 27 '13 at 01:47
  • How to actually run the MySuite class? – java123999 Nov 14 '16 at 11:58
  • This is so inconvenient when compared to TestNg test suite xml file. Why does Junit not allow us to select methods without annotating the test methods ? Has this inconvenience been fixed in Junit 5? – MasterJoe Apr 14 '20 at 18:41