Kyle Stoflet Kyle Stoflet - 7 months ago 23
Java Question

Why use JUnit test suites?

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.

Answer

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.