0

Why does a JUnit Suite class, in my cases its called TestSuite.class, not execute its own Test, Before, and After annotations? It only exectutes its own BeforeClass, AfterClass, and then ALL annotations of the suite test classes. I proved this is the case by creating a test project around this theory: https://gist.github.com/djangofan/5033350

Can anyone refer me to where this is explained? I need to really understand this.

djangofan
  • 28,471
  • 61
  • 196
  • 289

3 Answers3

1

Because a TestSuite is not a Test itself. Those annotation are for unit tests only. See here for an example.

public class FeatureTestSuite {
  // the class remains empty <----- important for your question
}
Andrew White
  • 52,720
  • 19
  • 113
  • 137
  • In this case my test suite is annotated with @Test and so, in this case it is a test and is also not just a simple test suite, its a test suite + tests. – djangofan Feb 26 '13 at 02:26
  • Can you provide a link to that? I looked everywhere. Nothing that I found says that you cannot have a suite that contains its own tests. – djangofan Feb 26 '13 at 15:45
  • 1
    Yeah, the link in my answer explicit states in in the example code that I copied. The source code is pretty straight forward too. https://github.com/junit-team/junit/blob/master/src/main/java/org/junit/runners/Suite.java – Andrew White Feb 26 '13 at 16:25
  • Thanks. My finished code for testing this question is at: https://github.com/djangofan/JUnitSuiteCallOrder – djangofan Mar 04 '13 at 15:54
0

A TestSuite is way of identifying a group of tests you wish apply some common behaviour to.

Perhaps better explained with an example.

So say you were doing some basic CRUD tests on an Orders Table in a Database MyDB. Everyone needs mydb to be there and the orders table to exist, so you put them in a suite. It sets up the db and the table, the tests run, then before the suite goes out of scope the db is dropped, everything is nice and clean for the next test run. Otherwise you'd have to do that in every test which is expensive, or worse have test data from previous tests cause the others to fail often apparently randomly as you would have created an implicit dependancy between them. There are other ways of achieving the same thing, but they clutter up your tests and you have to remember to call them.

You don't have to test it. If it doesn't get done none of your tests will execute.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0

As others have said, it's because a TestSuite is not a Test. It's just a class with an annotation to group other tests, so that it is more convenient to run.

It does have one special property, however, and that is the execution of @BeforeClass and @AfterClass. These are enabled to allow a global setup/teardown for the suite. It does not execute any tests (including @After, @Before or any rules).

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171