Home > OS >  JUnit 5 TestExecutionListeners and intermediate containers
JUnit 5 TestExecutionListeners and intermediate containers

Time:10-29

So, I came across this statement in the documentation of JUnit5's TestExecutionListener: "Contrary to JUnit 4, test engines are supposed to report events not only for identifiers that represent executable leaves in the test plan but also for all intermediate containers." The doc link -> https://junit.org/junit5/docs/5.0.3/api/org/junit/platform/launcher/TestExecutionListener.html

My question is: what are intermediate containers?

CodePudding user response:

Any JUnit 5 test engine can determine how the tree of tests is constructed. A TestIdentifier describes one node in this tree and it is one of three types:

  • CONTAINER
  • TEST
  • CONTAINER_AND_TEST

A test can be executed, whereas a container has children, which can themselves be of any of the three types.

Let's look at an example using JUnit Jupiter:

class MyTestContainer {
  @Test void test1() { }

  @Nested
  class InnerTestContainer {
     @Test void test2() { }
  }
}

Running this would announce the following events - maybe in a slightly different order:

  • execution started: MyTestContainer
  • execution started: MyTestContainer.test1
  • execution finished: MyTestContainer.test1
  • execution started: MyTestContainer.InnerTestContainer
  • execution started: MyTestContainer.InnerTestContainer.test2
  • execution finished: MyTestContainer.InnerTestContainer.test2
  • execution finished: MyTestContainer.InnerTestContainer
  • execution finished: MyTestContainer

Keep in mind that it's completely up to the test engines how they define containers and executable tests. Some (ArchUnit) use member variables for tests, others build up the hierarchy through a DSL of their own. Cucumber, for example, has its own way to group and nest features, specifications etc.

Hope I could clarify the idea behind test, container and the tree of those forming the final test plan.

  • Related