Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouping JUnit tests

Is there any way to group tests in JUnit, so that I can run only some groups?

Or is it possible to annotate some tests and then globally disable them?

I'm using JUnit 4, I can't use TestNG.

edit: @RunWith and @SuiteClasses works great. But is it possible to annotate like this only some tests in test class? Or do I have to annotate whole test class?

like image 298
Jakub Arnold Avatar asked May 03 '09 14:05

Jakub Arnold


People also ask

Can we group tests 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.

Which annotation is used for grouping multiple tests for a method in JUnit?

class) This annotation is helpful whenever we want to test multiple classes at once.

Which supports grouping of test cases?

TestNG Groups allow you to perform groupings of different test methods. Grouping of test methods is required when you want to access the test methods of different classes.

What are nested tests used for?

@Nested : the solution @Nested addresses this issue by giving the possibility to group multiple test methods inside multiple nested classes of a main(outer) test class. The test methods of all nested classes defined in the main(outer) test class are handled as any test methods.


2 Answers

JUnit 4.8 supports grouping:

public interface SlowTests {} public interface IntegrationTests extends SlowTests {} public interface PerformanceTests extends SlowTests {} 

And then...

public class AccountTest {      @Test     @Category(IntegrationTests.class)     public void thisTestWillTakeSomeTime() {         ...     }      @Test     @Category(IntegrationTests.class)     public void thisTestWillTakeEvenLonger() {         ...     }      @Test     public void thisOneIsRealFast() {         ...     } } 

And lastly,

@RunWith(Categories.class) @ExcludeCategory(SlowTests.class) @SuiteClasses( { AccountTest.class, ClientTest.class }) public class UnitTestSuite {} 

Taken from here: https://community.oracle.com/blogs/johnsmart/2010/04/25/grouping-tests-using-junit-categories-0

Also, Arquillian itself supports grouping: https://github.com/weld/core/blob/master/tests-arquillian/src/test/java/org/jboss/weld/tests/Categories.java

like image 122
Ondra Žižka Avatar answered Oct 21 '22 12:10

Ondra Žižka


Do you want to group tests inside a test class or do you want to group test classes? I am going to assume the latter.

It depends on how you are running your tests. If you run them by Maven, it is possible to specify exactly what tests you want to include. See the Maven surefire documentation for this.

More generally, though, what I do is that I have a tree of test suites. A test suite in JUnit 4 looks something like:

 @RunWith(Suite.class)  @SuiteClasses({SomeUnitTest1.class, SomeUnitTest2.class})  public class UnitTestsSuite {  } 

So, maybe I have a FunctionTestsSuite and a UnitTestsSuite, and then an AllTestsSuite which includes the other two. If you run them in Eclipse you get a very nice hierarchical view.

The problem with this approach is that it's kind of tedious if you want to slice tests in more than one different way. But it's still possible (you can for example have one set of suites that slice based on module, then another slicing on the type of test).

like image 25
waxwing Avatar answered Oct 21 '22 14:10

waxwing