Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run all tests belonging to a certain Category in JUnit 4

JUnit 4.8 contains a nice new feature called "Categories" that allows you to group certain kinds of tests together. This is very useful, e.g. to have separate test runs for slow and fast tests. I know the stuff mentioned in JUnit 4.8 release notes, but would like to know how I can actually run all the tests annotated with certain category.

The JUnit 4.8 release notes show an example suite definition, where SuiteClasses annotation selects the tests from certain category to run, like this:

@RunWith(Categories.class) @IncludeCategory(SlowTests.class) @SuiteClasses( { A.class, B.class }) // Note that Categories is a kind of Suite public class SlowTestSuite {   // Will run A.b and B.c, but not A.a } 

Does anyone know how I could run all the tests in SlowTests category? It seems that you must have the SuiteClasses annotation...

like image 858
Kaitsu Avatar asked Feb 01 '10 12:02

Kaitsu


People also ask

How do you run all test cases in JUnit?

Right clicking on this class and selecting Run As JUnit test runs all of the tests in the specified directory including all tests in nested subfolders.

Is it possible to group the test cases in JUnit?

JUnit test suites help to grouping and executing tests in bulk. Executing tests separately for all test classes is not desired in most cases. Test suites help in achieving this grouping. In JUnit, test suites can be created and executed with these annotations.


1 Answers

I found out one possible way to achieve what I want, but I don't consider this to be the best possible solution as it relies on ClassPathSuite library that is not part of JUnit.

I define the test suite for slow tests like this:

@RunWith(Categories.class) @Categories.IncludeCategory(SlowTests.class) @Suite.SuiteClasses( { AllTests.class }) public class SlowTestSuite { } 

AllTests class is defined like this:

@RunWith(ClasspathSuite.class) public class AllTests { } 

I had to use ClassPathSuite class from ClassPathSuite project here. It will find all the classes with tests.

like image 103
Kaitsu Avatar answered Oct 08 '22 05:10

Kaitsu