0
0
JUnittesting~5 mins

Test suites with @Suite in JUnit

Choose your learning style9 modes available
Introduction

Test suites help you run many test classes together in one go. This saves time and keeps tests organized.

When you want to run all tests for a feature at once.
When you have multiple test classes for different parts of your app and want to run them together.
When you want to group slow tests separately from fast tests.
When you want to run all tests before a release to check everything works.
When you want to organize tests by modules or layers in your project.
Syntax
JUnit
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({TestClass1.class, TestClass2.class})
public class MyTestSuite {
}

The @Suite annotation marks the class as a test suite.

@SelectClasses lists the test classes to run in this suite.

Examples
This suite runs tests from LoginTests and SignupTests classes together.
JUnit
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({LoginTests.class, SignupTests.class})
public class UserTestsSuite {
}
This suite runs only the DatabaseTests class.
JUnit
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({DatabaseTests.class})
public class DatabaseTestSuite {
}
Sample Program

This example has two test classes each with one simple test. The suite MyTestSuite runs both classes together.

JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class TestClass1 {
    @Test
    void testOne() {
        assertTrue(1 + 1 == 2);
    }
}

public class TestClass2 {
    @Test
    void testTwo() {
        assertTrue("hello".startsWith("h"));
    }
}

import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasses({TestClass1.class, TestClass2.class})
public class MyTestSuite {
}
OutputSuccess
Important Notes

Test classes included in the suite must be public and have valid test methods.

You can add more test classes by listing them in @SelectClasses.

Use your IDE or build tool to run the suite class as a JUnit test.

Summary

Test suites group multiple test classes to run together.

Use @Suite and @SelectClasses annotations to create a suite.

Suites help organize and save time when running many tests.