Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a container in JUnit 5

Tags:

java

junit5

The current https://junit.org/junit5/docs/current/user-guide uses the term container around 50 times without defining it.

What is the difference between test class and container? Can one container be in more than one class? Can one class has more than one container? (for example nested classes).

like image 248
Alexei Vinogradov Avatar asked Mar 03 '20 12:03

Alexei Vinogradov


2 Answers

Let me chime in as one of the original members of the JUnit 5 team...

JUnit 5, as a platform, allows deeply nested trees of containers and tests. You can even have nodes that are both a test (= it makes sense to run it individually) and a container (= it has children). At the root of any testengine‘s Test tree is always the engine container. There’s one important thing to know: Containers without children that are not themselves also tests will be pruned and not run!

As for Jupiter you have classes and nested classes as containers and test methods as tests. Consider the case of dynamic tests, though, where the annotated method is a test, I think, but it also creates a lot of children which are themselves tests. So it’s also a container.

Other test engines might decide to create containers for the packages and parent packages as well.

like image 86
johanneslink Avatar answered Oct 16 '22 09:10

johanneslink


I suppose that container here is something containing tests :))

If you debug an executionStarted method in any listener you will see that top level container called "[engine:junit-jupiter]" (for the standard runner, or any other engine name you're using).

Then you'll see a test classes (each of them is container). If you're using "nested" (I mean @Nested tests) feature, your upper level tests will be also shown as container.

You can add more levels by creating your own engine and overriding test discovery mechanism.

like image 23
Eduard Dubilyer Avatar answered Oct 16 '22 11:10

Eduard Dubilyer