28

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.

Kyle Stoflet
  • 1,194
  • 3
  • 16
  • 27
  • 4
    (To the person voting to close it) Good grief. The question isn't too broad. It is perfectly fine! – TofuBeer Apr 27 '16 at 22:08
  • Possible duplicate of [What is TestSuite?](http://stackoverflow.com/questions/7250257/what-is-testsuite) – kryger Apr 27 '16 at 22:11
  • @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. – Kyle Stoflet Apr 27 '16 at 22:12

1 Answers1

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.

Sleiman Jneidi
  • 22,907
  • 14
  • 56
  • 77
  • 6
    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. – Bill Frasure Apr 27 '16 at 22:40
  • 1
    @BillFrasure Now that I've come across integration tests here at work, this makes perfect sense. Thanks! – Kyle Stoflet May 06 '16 at 18:02