Test Engine SPI

The Test Engine SPI allows for creating test executors on top of the JVM. In the JUnit 5 framework, there are two Test Engine implementations out of the box:

  • The junit-vintage-engine: This allows running JUnit 3 and 4 tests in the JUnit platform.
  • The junit-jupiter-engine: This allows running JUnit 5 tests in the JUnit platform.

Moreover, third-party test libraries (for example, Spock, TestNG, and so on) can plug into the JUnit Platform by providing a custom Test Engine. To do that, these frameworks should create its own Test Engine by extending the JUnit 5 interface org.junit.platform.engine.TestEngine. In order to extend this interface, three mandatory methods must be overridden:

  • getId: The unique identifier for the test engine.
  • discover: The logic to find and filter the test(s).
  • execute: The logic to run the previously found test(s).

The following example provides the skeleton for a custom Test Engine:

package io.github.bonigarcia;

import org.junit.platform.engine.EngineDiscoveryRequest;
import org.junit.platform.engine.ExecutionRequest;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.TestEngine;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.EngineDescriptor;

public class MyCustomEngine implements TestEngine {

public static final String ENGINE_ID = "my-custom-engine";

@Override
public String getId() {
return ENGINE_ID;
}

@Override
public TestDescriptor discover(EngineDiscoveryRequest discoveryRequest,
UniqueId uniqueId) {
// Discover test(s) and return a TestDescriptor object
TestDescriptor testDescriptor = new EngineDescriptor(uniqueId,
"My test");
return testDescriptor;
}

@Override
public void execute(ExecutionRequest request) {
// Use ExecutionRequest to execute TestDescriptor
TestDescriptor rootTestDescriptor =
request.getRootTestDescriptor();
request.getEngineExecutionListener()
.executionStarted(rootTestDescriptor);
}

}
A list of existing Test Engines (for example, Specsy, Spek, and others) is maintained by the community in the wiki located in the GitHub site of the JUnit 5 team: https://github.com/junit-team/junit5/wiki/Third-party-Extensions.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.123.189