Why use JUnit test suites?
Asked Answered
P

1

29

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.

Positivism answered 27/4, 2016 at 21:59 Comment(3)
(To the person voting to close it) Good grief. The question isn't too broad. It is perfectly fine!Dyslogistic
Possible duplicate of What is TestSuite?Brucine
@kryger, I saw that as well as a couple others. I'm more interested in a practical use, and an advantage over tests. Seems like other answers simply state what a test suite is. I understand that much, but I'm trying to put it into realistic terms and actually understand why you'd want to bundle tests. I hope this makes more sense.Positivism
T
31

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.

Typescript answered 27/4, 2016 at 22:4 Comment(2)
In addition to DB tests, another usual grouping is Unit tests vs Integration tests. Unit tests can be nice to have running all the time, whereas Integration tests that leverage many parts of the system can be overkill.Caruthers
@BillFrasure Now that I've come across integration tests here at work, this makes perfect sense. Thanks!Positivism

© 2022 - 2024 — McMap. All rights reserved.