I'm going to be implementing some unit tests using JUnit in some upcoming tasks for work. I have slight experience with JUnit from my previous employer, but while I was studying, I came across test suites. I don't have any idea if I'll be using them, but I was particularly interested in why they even exist.
Can someone give me a practical situation where I'd ever want to use a test suite, and also, what are advantages of using test suites as opposed to just using various independent tests?
EDIT: I feel this question isn't a duplicate since other answers to similar questions give a general definition, or a code snippet (as far as I could see) and I'm looking for a more practical use of suites from a design perspective. I'd like to know why bundling tests together may be a good idea, or why it may not be, etc.
Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @Suite annotations are used to run the suite tests. This chapter takes an example having two test classes, TestJunit1 & TestJunit2, that run together using Test Suite.
Test suites are also useful for the following types of tests: Build verification tests: A collection of test cases that perform a basic validation of most the functional areas in the product. The tests are executed after each product build and before the build is promoted for use by a larger audience.
Using Suite as a runner allows you to manually build a suite containing tests from many classes. It is the JUnit 4 equivalent of the JUnit 3.8. x static Test suite() method. To use it, annotate a class with @RunWith(Suite.
JUnit is elegantly simple. It is less complex and takes less time. JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results.
JUnit - Suite Test. Test suite is used to bundle a few unit test cases and run them together. In JUnit, both @RunWith and @Suite annotations are used to run the suite tests.
JUnit is elegantly simple. It is less complex and takes less time. JUnit tests can be run automatically and they check their own results and provide immediate feedback. There's no need to manually comb through a report of test results. JUnit tests can be organized into test suites containing test cases and even other test suites.
JUnit 5 test suites are written with @Suite annotation. Suites help us run the tests spread into multiple classes and packages. We can use Include** and Exclude** annotations (discussed later in this tutorial) for filtering test packages, test classes or even test methods.
We can use JUnit 5 test suites to run tests spread into multiple test classes and different packages. JUnit 5 provides out of box two annotations: @SelectPackages and @SelectClasses to create test suites. Additionally, we may use other annotations for filtering test packages, classes, or even test methods.
Suites allow you to run multiple test classes where you can execute routines before and after the entire suite.
Testing that requires a database setup comes to mind,
Unload in memory database when the entire suite is done.
@RunWith(Suite.class) @Suite.SuiteClasses({ MyUnitTests.class }) public class MySuiteTest { @ClassRule public static H2TestRule h2TestRule = new H2TestRule(); }
Here H2TestRule
is a Rule
for the entire suite rather than a single test case.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With