Grouping Junit Tests

Grouping JUnit tests

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).

How to use junit 5 group test(gather user cases) in a beautiful way

I'm not aware of anything in JUnit5 that looks as expressive and concise as tests in javascript you showed. But there is at least one thing to consider that would allow you to group tests in a similar way.

It is @Nested annotation. It allows you to group a set of tests into a logical group. Once your test pack is executed it will be easier to navigate and understand which groups of tests passed/failed. Quite useful if you have a big set of tests within your JUnit test case class.
Here is an example of how it looks like:

public class YourTestCase {


@Nested
@DisplayName("Tests for my method")
class MyMethodUseCases {
@Test
void testUseCase1() {
...
}

@Test
void testUseCase2() {
...
}

@Test
void testUseCase3() {
...
}
}

If your group of tests shares some context that needs to be set up before tests executed then you might also consider a special JUnit runner for that like
HierarchicalContextRunner. This is not a part of standard JUnit, but a custom extension.

And the last thing, if the only difference between your use cases is data input, then you might consider to use @ParametrizedTest annotation. It allows you to write one test and execute it with multiple data inputs.

Grouping tests that are located in the same class

You can call your test in a function that represent a group :

@Test
public void simpleTests() {
testSimpleRow();
testSimpleCol();
testBasicTie();
}

@Test
public void higgerTests() {
testHigherRow();
testHigherCol();
}

This way you are going to execute each other method in a bigger test method.

Don't forget to remove @Test annotation on other tests.

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

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.



Related Topics



Leave a reply



Submit