Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use JUnit test suites?

Tags:

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.

like image 386
Kyle Stoflet Avatar asked Apr 27 '16 21:04

Kyle Stoflet


People also ask

What is the use of test suite in JUnit?

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.

Why do we use 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.

What are the uses of Org JUnit runners suite class?

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.

What are the reasons and benefits for using JUnit for testing?

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.

What is @suite test In JUnit?

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.

What is the advantage of using JUnit?

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.

What is @suite annotation in JUnit 5?

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.

How do I run multiple tests in JUnit 5?

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.


1 Answers

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,

  1. load a database in memory (you can't afford to do that before and after every unit test)
  2. Run all test cases
  3. 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.

like image 131
Sleiman Jneidi Avatar answered Sep 20 '22 19:09

Sleiman Jneidi