Introduction to the technical principles of Junit Platform Launcher framework in Java
Junit Platform Launcher is an important component in Junit 5. It is responsible for loading and executing test operators, and coordinating interaction between different test frameworks.This article will introduce the technical principles of the Junit Platform Launcher framework.
JUNIT PLATFORM LAUNCHER is a Java application that acts as a test execution engine.It executes test cases through three key steps: discover test engines, discover test plans, and perform test plans.
In the steps of discovering the test engine, Junit Platform Launcher will search for all Meta-INF/Services/Org.junit.Platform.etenstentery files on the road path and load the test engine.These test engines must implement the TESTENGINE interface, which defines the behavior and functions of the test engine.Junit 5 comes with a test engine called Junit-Jupiter-Engine, which can perform testing based on the Junit Jupiter test framework.
Once all the test engines were found and loaded, Junit Platform Launcher began to discover the test plan.The test plan is a set of test executing descriptor generated by the test engine.The test execution descriptor is an object that describes the test case to be executed and its related attributes (such as labels, timeout, etc.).Each test engine generates a set of test execution descriptors.
Before performing the test plan, Junit Platform Launcher sorts the test execution descriptor.The purpose of sorting is to determine the execution order of the test case.Sorting depends on the specific rules defined by the test engine, which can be sorted based on the name of the test case, label, etc.
Finally, in the step of performing the test plan, the janit platform Launcher traverses the test execution descriptor and uses the corresponding test engine to perform each test case.The test engine will be responsible for running test cases and generate execution results.Junit Platform Launcher will capture these results and provide them to users.
The following is an example. It demonstrates how to use Junit Platform Launcher to perform the test:
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestLauncher;
import org.junit.platform.launcher.TestPlan;
public class LauncherExample {
public static void main(String[] args) {
TestLauncher launcher = TestLauncherFactory.create();
TestexecuCUTIONENER LISTENER = YOURCUSTOSMLISTENER (); // Custom test monitor
launcher.registerTestExecutionListeners(listener);
// Discover the test plan
TestPlan testPlan = launcher.discover();
// Execute the test plan
launcher.execute(testPlan);
}
}
In the above example, we create a testlauncher by calling the Create method of the TestlauncherFactory class.We can then register a custom test monitor and use the discover method to find the test plan.Finally, we call the Execute method to execute the test plan.
To sum up, Junit Platform Launcher is a key component in Junit 5. It is responsible for loading and executing test operators, and coordinating interaction between different test frameworks.By discovering the three steps of test engines, discovery test plans, and execution test programs, Junit Platform Launcher implemented the execution and results collection of test cases.