Why does a JUnit suite class not execute its own Test, Before, and After annotations?

StackOverflow https://stackoverflow.com/questions/15078241

  •  11-03-2022
  •  | 
  •  

Pregunta

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.

¿Fue útil?

Solución

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
}

Otros consejos

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.

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).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top