|
16 | 16 | import static org.apiguardian.api.API.Status.EXPERIMENTAL;
|
17 | 17 | import static org.apiguardian.api.API.Status.MAINTAINED;
|
18 | 18 | import static org.apiguardian.api.API.Status.STABLE;
|
| 19 | +import static org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.Phase.DISCOVERY; |
19 | 20 | import static org.junit.platform.launcher.core.EngineDiscoveryOrchestrator.Phase.EXECUTION;
|
20 | 21 |
|
21 | 22 | import java.nio.file.Path;
|
| 23 | +import java.util.List; |
22 | 24 | import java.util.Map;
|
23 | 25 | import java.util.ServiceLoader;
|
24 | 26 | import java.util.stream.Stream;
|
|
29 | 31 | import org.junit.platform.commons.util.CollectionUtils;
|
30 | 32 | import org.junit.platform.commons.util.Preconditions;
|
31 | 33 | import org.junit.platform.engine.DiscoveryFilter;
|
| 34 | +import org.junit.platform.engine.DiscoveryIssue; |
32 | 35 | import org.junit.platform.engine.DiscoverySelector;
|
33 | 36 | import org.junit.platform.engine.EngineDiscoveryRequest;
|
34 | 37 | import org.junit.platform.engine.EngineExecutionListener;
|
|
46 | 49 | import org.junit.platform.launcher.core.ServiceLoaderTestEngineRegistry;
|
47 | 50 |
|
48 | 51 | /**
|
49 |
| - * {@code EngineTestKit} provides support for executing a test plan for a given |
50 |
| - * {@link TestEngine} and then accessing the results via |
51 |
| - * {@linkplain EngineExecutionResults a fluent API} to verify the expected results. |
| 52 | + * {@code EngineTestKit} provides support for discovering and executing tests |
| 53 | + * for a given {@link TestEngine} and provides convenient access to the results. |
| 54 | + * |
| 55 | + * <p>For <em>discovery</em>, {@link EngineDiscoveryResults} provides access to |
| 56 | + * the {@link TestDescriptor} of the engine and any {@link DiscoveryIssue |
| 57 | + * DiscoveryIssues} that were encountered. |
| 58 | + * |
| 59 | + * <p>For <em>execution</em>, {@link EngineExecutionResults} provides a fluent |
| 60 | + * API to verify the expected results. |
52 | 61 | *
|
53 | 62 | * @since 1.4
|
54 | 63 | * @see #engine(String)
|
55 | 64 | * @see #engine(TestEngine)
|
| 65 | + * @see #discover(String, LauncherDiscoveryRequest) |
| 66 | + * @see #discover(TestEngine, LauncherDiscoveryRequest) |
56 | 67 | * @see #execute(String, LauncherDiscoveryRequest)
|
57 | 68 | * @see #execute(TestEngine, LauncherDiscoveryRequest)
|
| 69 | + * @see EngineDiscoveryResults |
58 | 70 | * @see EngineExecutionResults
|
59 | 71 | */
|
60 | 72 | @API(status = MAINTAINED, since = "1.7")
|
@@ -121,6 +133,65 @@ public static Builder engine(TestEngine testEngine) {
|
121 | 133 | return new Builder(testEngine);
|
122 | 134 | }
|
123 | 135 |
|
| 136 | + /** |
| 137 | + * Discover tests for the given {@link LauncherDiscoveryRequest} using the |
| 138 | + * {@link TestEngine} with the supplied ID. |
| 139 | + * |
| 140 | + * <p>The {@code TestEngine} will be loaded via Java's {@link ServiceLoader} |
| 141 | + * mechanism, analogous to the manner in which test engines are loaded in |
| 142 | + * the JUnit Platform Launcher API. |
| 143 | + * |
| 144 | + * <p>{@link org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder} |
| 145 | + * provides a convenient way to build an appropriate discovery request to |
| 146 | + * supply to this method. As an alternative, consider using |
| 147 | + * {@link #engine(TestEngine)} for a more fluent API. |
| 148 | + * |
| 149 | + * @param engineId the ID of the {@code TestEngine} to use; must not be |
| 150 | + * {@code null} or <em>blank</em> |
| 151 | + * @param discoveryRequest the {@code LauncherDiscoveryRequest} to use |
| 152 | + * @return the results of the discovery |
| 153 | + * @throws PreconditionViolationException for invalid arguments or if the |
| 154 | + * {@code TestEngine} with the supplied ID cannot be loaded |
| 155 | + * @since 1.13 |
| 156 | + * @see #discover(TestEngine, LauncherDiscoveryRequest) |
| 157 | + * @see #engine(String) |
| 158 | + * @see #engine(TestEngine) |
| 159 | + */ |
| 160 | + @API(status = EXPERIMENTAL, since = "1.13") |
| 161 | + public static EngineDiscoveryResults discover(String engineId, LauncherDiscoveryRequest discoveryRequest) { |
| 162 | + Preconditions.notBlank(engineId, "TestEngine ID must not be null or blank"); |
| 163 | + return discover(loadTestEngine(engineId.trim()), discoveryRequest); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Discover tests for the given {@link LauncherDiscoveryRequest} using the |
| 168 | + * supplied {@link TestEngine}. |
| 169 | + * |
| 170 | + * <p>{@link org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder} |
| 171 | + * provides a convenient way to build an appropriate discovery request to |
| 172 | + * supply to this method. As an alternative, consider using |
| 173 | + * {@link #engine(TestEngine)} for a more fluent API. |
| 174 | + * |
| 175 | + * @param testEngine the {@code TestEngine} to use; must not be {@code null} |
| 176 | + * @param discoveryRequest the {@code EngineDiscoveryResults} to use; must |
| 177 | + * not be {@code null} |
| 178 | + * @return the recorded {@code EngineExecutionResults} |
| 179 | + * @throws PreconditionViolationException for invalid arguments |
| 180 | + * @since 1.13 |
| 181 | + * @see #discover(String, LauncherDiscoveryRequest) |
| 182 | + * @see #engine(String) |
| 183 | + * @see #engine(TestEngine) |
| 184 | + */ |
| 185 | + @API(status = EXPERIMENTAL, since = "1.13") |
| 186 | + public static EngineDiscoveryResults discover(TestEngine testEngine, LauncherDiscoveryRequest discoveryRequest) { |
| 187 | + Preconditions.notNull(testEngine, "TestEngine must not be null"); |
| 188 | + Preconditions.notNull(discoveryRequest, "EngineDiscoveryRequest must not be null"); |
| 189 | + LauncherDiscoveryResult discoveryResult = discover(testEngine, discoveryRequest, DISCOVERY); |
| 190 | + TestDescriptor engineDescriptor = discoveryResult.getEngineTestDescriptor(testEngine); |
| 191 | + List<DiscoveryIssue> discoveryIssues = discoveryResult.getDiscoveryIssues(testEngine); |
| 192 | + return new EngineDiscoveryResults(engineDescriptor, discoveryIssues); |
| 193 | + } |
| 194 | + |
124 | 195 | /**
|
125 | 196 | * Execute tests for the given {@link EngineDiscoveryRequest} using the
|
126 | 197 | * {@link TestEngine} with the supplied ID.
|
@@ -260,13 +331,16 @@ private static void executeDirectly(TestEngine testEngine, EngineDiscoveryReques
|
260 | 331 |
|
261 | 332 | private static void executeUsingLauncherOrchestration(TestEngine testEngine,
|
262 | 333 | LauncherDiscoveryRequest discoveryRequest, EngineExecutionListener listener) {
|
263 |
| - LauncherDiscoveryResult discoveryResult = new EngineDiscoveryOrchestrator(singleton(testEngine), |
264 |
| - emptySet()).discover(discoveryRequest, EXECUTION); |
265 |
| - TestDescriptor engineTestDescriptor = discoveryResult.getEngineTestDescriptor(testEngine); |
266 |
| - Preconditions.notNull(engineTestDescriptor, "TestEngine did not yield a TestDescriptor"); |
| 334 | + LauncherDiscoveryResult discoveryResult = discover(testEngine, discoveryRequest, EXECUTION); |
267 | 335 | new EngineExecutionOrchestrator().execute(discoveryResult, listener);
|
268 | 336 | }
|
269 | 337 |
|
| 338 | + private static LauncherDiscoveryResult discover(TestEngine testEngine, LauncherDiscoveryRequest discoveryRequest, |
| 339 | + EngineDiscoveryOrchestrator.Phase phase) { |
| 340 | + return new EngineDiscoveryOrchestrator(singleton(testEngine), emptySet()) // |
| 341 | + .discover(discoveryRequest, phase); |
| 342 | + } |
| 343 | + |
270 | 344 | @SuppressWarnings("unchecked")
|
271 | 345 | private static TestEngine loadTestEngine(String engineId) {
|
272 | 346 | Iterable<TestEngine> testEngines = new ServiceLoaderTestEngineRegistry().loadTestEngines();
|
@@ -446,6 +520,25 @@ public Builder outputDirectoryProvider(OutputDirectoryProvider outputDirectoryPr
|
446 | 520 | return this;
|
447 | 521 | }
|
448 | 522 |
|
| 523 | + /** |
| 524 | + * Discover tests for the configured {@link TestEngine}, |
| 525 | + * {@linkplain DiscoverySelector discovery selectors}, |
| 526 | + * {@linkplain DiscoveryFilter discovery filters}, and |
| 527 | + * <em>configuration parameters</em>. |
| 528 | + * |
| 529 | + * @return the recorded {@code EngineDiscoveryResults} |
| 530 | + * @since 1.13 |
| 531 | + * @see #selectors(DiscoverySelector...) |
| 532 | + * @see #filters(Filter...) |
| 533 | + * @see #configurationParameter(String, String) |
| 534 | + * @see #configurationParameters(Map) |
| 535 | + */ |
| 536 | + @API(status = EXPERIMENTAL, since = "1.13") |
| 537 | + public EngineDiscoveryResults discover() { |
| 538 | + LauncherDiscoveryRequest request = this.requestBuilder.build(); |
| 539 | + return EngineTestKit.discover(this.testEngine, request); |
| 540 | + } |
| 541 | + |
449 | 542 | /**
|
450 | 543 | * Execute tests for the configured {@link TestEngine},
|
451 | 544 | * {@linkplain DiscoverySelector discovery selectors},
|
|
0 commit comments