You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It seems that the latest JUnit version, JUnit 5, includes a module 'JUnit Platform' which:
is responsible for running and discovering tests. This API is essential for third-party tools (e.g. IntelliJ Idea or NetBeans), as well as build tools (e.g. Maven or Gradle). It provides the TestEngine API, which is responsible for executing tests. The normal application developer, who is writing tests for a certain application, does not need to deal directly with the platform API.
Here are two examples on how to use the JUnit Platform to find and run tests.
/* * Copyright 2015-2020 the original author or authors. * * All rights reserved. This program and the accompanying materials are * made available under the terms of the Eclipse Public License v2.0 which * accompanies this distribution and is available at * * https://www.eclipse.org/legal/epl-v20.html */packageexample;
// tag::imports[]importstaticorg.junit.platform.engine.discovery.ClassNameFilter.includeClassNamePatterns;
importstaticorg.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
importstaticorg.junit.platform.engine.discovery.DiscoverySelectors.selectPackage;
importjava.io.PrintWriter;
importjava.nio.file.Path;
importjava.nio.file.Paths;
importorg.junit.platform.launcher.Launcher;
importorg.junit.platform.launcher.LauncherDiscoveryRequest;
importorg.junit.platform.launcher.TestExecutionListener;
importorg.junit.platform.launcher.TestPlan;
importorg.junit.platform.launcher.core.LauncherConfig;
importorg.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
importorg.junit.platform.launcher.core.LauncherFactory;
importorg.junit.platform.launcher.listeners.SummaryGeneratingListener;
importorg.junit.platform.launcher.listeners.TestExecutionSummary;
importorg.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener;
// end::imports[]/** * @since 5.0 */classUsingTheLauncherDemo {
@org.junit.jupiter.api.Test@SuppressWarnings("unused")
voiddiscovery() {
// @formatter:off// tag::discovery[]LauncherDiscoveryRequestrequest = LauncherDiscoveryRequestBuilder.request()
.selectors(
selectPackage("com.example.mytests"),
selectClass(MyTestClass.class)
)
.filters(
includeClassNamePatterns(".*Tests")
)
.build();
Launcherlauncher = LauncherFactory.create();
TestPlantestPlan = launcher.discover(request);
// end::discovery[]// @formatter:on
}
@org.junit.jupiter.api.Test@SuppressWarnings("unused")
voidexecution() {
// @formatter:off// tag::execution[]LauncherDiscoveryRequestrequest = LauncherDiscoveryRequestBuilder.request()
.selectors(
selectPackage("com.example.mytests"),
selectClass(MyTestClass.class)
)
.filters(
includeClassNamePatterns(".*Tests")
)
.build();
Launcherlauncher = LauncherFactory.create();
// Register a listener of your choiceSummaryGeneratingListenerlistener = newSummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
launcher.execute(request);
TestExecutionSummarysummary = listener.getSummary();
// Do something with the TestExecutionSummary.// end::execution[]// @formatter:on
}
@org.junit.jupiter.api.TestvoidlauncherConfig() {
PathreportsDir = Paths.get("target", "xml-reports");
PrintWriterout = newPrintWriter(System.out);
// @formatter:off// tag::launcherConfig[]LauncherConfiglauncherConfig = LauncherConfig.builder()
.enableTestEngineAutoRegistration(false)
.enableTestExecutionListenerAutoRegistration(false)
.addTestEngines(newCustomTestEngine())
.addTestExecutionListeners(newLegacyXmlReportGeneratingListener(reportsDir, out))
.addTestExecutionListeners(newCustomTestExecutionListener())
.build();
Launcherlauncher = LauncherFactory.create(launcherConfig);
LauncherDiscoveryRequestrequest = LauncherDiscoveryRequestBuilder.request()
.selectors(selectPackage("com.example.mytests"))
.build();
launcher.execute(request);
// end::launcherConfig[]// @formatter:on
}
}
classMyTestClass {
}
classCustomTestExecutionListenerimplementsTestExecutionListener {
}
Can replace our module to find and run tests with this 'platform'? What are the advantages and disadvantages? Does it allow to run individual unit test cases rather than test classes? This last question is quite important to GZoltar, as it must collect coverage per unit test, and according to JUnit 5 documentation it might be possible to achieve that out-of-the-box.
The text was updated successfully, but these errors were encountered:
It seems that the latest JUnit version, JUnit 5, includes a module 'JUnit Platform' which:
Here are two examples on how to use the JUnit Platform to find and run tests.
Example found on stackoverflow
Official example
Can replace our module to find and run tests with this 'platform'? What are the advantages and disadvantages? Does it allow to run individual unit test cases rather than test classes? This last question is quite important to GZoltar, as it must collect coverage per unit test, and according to JUnit 5 documentation it might be possible to achieve that out-of-the-box.
The text was updated successfully, but these errors were encountered: