Skip to content

[JUnit] Add JUnit Platform @Cucumber annotation #1824

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.cucumber.examples.junit5.calculator;

import io.cucumber.junit.platform.engine.Cucumber;

@Cucumber
public class RunCucumberTest {
}
37 changes: 27 additions & 10 deletions junit-platform-engine/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Cucumber JUnit Platform Engine
==============================

Use JUnit Platform to execute cucumber scenarios.
Use JUnit Platform to execute Cucumber scenarios.

Add the `cucumber-junit-platform-engine` dependency to your `pom.xml`:

Expand All @@ -17,12 +17,34 @@ Add the `cucumber-junit-platform-engine` dependency to your `pom.xml`:
This will allow the IntelliJ IDEA, Eclipse, Maven, Gradle, ect, to discover,
select and execute Cucumber scenarios.

## Maven Surefire workaround ##
## Surefire and Gradle workarounds

Maven Surefire does not yet support discovery of non-class based test as a
workaround you can use the antrun plugin to start the the JUnit Platform
Maven Surefire and Gradle do not yet support discovery of non-class based tests
(see: [gradle/#4773](https://github.com/gradle/gradle/issues/4773),
[SUREFIRE-1724](https://issues.apache.org/jira/browse/SUREFIRE-1724)). As a
workaround you can either use the `@Cucumber` annotation or the JUnit Platform
Console Launcher.

### Use the @Cucumber annotation ###

Cucumber will scan the package of a class annotated with `@Cucumber` for feature
files.

```java
package com.example.app;

import io.cucumber.junit.platform.engine.Cucumber;

@Cucumber
public class RunCucumberTest {
}
```

### Use the JUnit Console Launcher ###

As a workaround you can use the JUnit Platform Console Launcher by using either
the Maven Antrun plugin or the Gradle JavaExec task.

```xml
<dependencies>
....
Expand Down Expand Up @@ -69,12 +91,6 @@ Console Launcher.
</plugins>
</build>
```
## Gradle Test workaround ##

Gradle Test does not yet support discovery of non-class based test ([gradle/#4773](https://github.com/gradle/gradle/issues/4773)).
As a work around you can use a custom task to start the the JUnit Platform
Console Launcher.

```groovy

tasks {
Expand Down Expand Up @@ -115,6 +131,7 @@ For supported values see [Constants](src/main/java/io/cucumber/junit/platform/en
Supported `DiscoverySelector`s are:
* `ClasspathRootSelector`
* `ClasspathResourceSelector`
* `ClassSelector`
* `PackageSelector`
* `FileSelector`
* `DirectorySelector`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.cucumber.junit.platform.engine;

import org.apiguardian.api.API;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Test discovery annotation. Marks the package of the annotated class for test
* discovery.
* <p>
* Some build tools do not support the {@link org.junit.platform.engine.discovery.DiscoverySelectors}
* used by Cucumber. As a work around Cucumber will scan the package of the
* annotated class for feature files and execute them.
*/
@API(status = API.Status.STABLE)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Cucumber {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.junit.platform.engine.EngineDiscoveryRequest;
import org.junit.platform.engine.Filter;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.discovery.ClassSelector;
import org.junit.platform.engine.discovery.ClasspathResourceSelector;
import org.junit.platform.engine.discovery.ClasspathRootSelector;
import org.junit.platform.engine.discovery.DirectorySelector;
Expand Down Expand Up @@ -35,6 +36,7 @@ private void resolve(EngineDiscoveryRequest request, TestDescriptor engineDescri

request.getSelectorsByType(ClasspathRootSelector.class).forEach(featureResolver::resolveClasspathRoot);
request.getSelectorsByType(ClasspathResourceSelector.class).forEach(featureResolver::resolveClasspathResource);
request.getSelectorsByType(ClassSelector.class).forEach(featureResolver::resolveClass);
request.getSelectorsByType(PackageSelector.class).forEach(featureResolver::resolvePackageResource);
request.getSelectorsByType(FileSelector.class).forEach(featureResolver::resolveFile);
request.getSelectorsByType(DirectorySelector.class).forEach(featureResolver::resolveDirectory);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.junit.platform.commons.logging.LoggerFactory;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.discovery.ClassSelector;
import org.junit.platform.engine.discovery.ClasspathResourceSelector;
import org.junit.platform.engine.discovery.ClasspathRootSelector;
import org.junit.platform.engine.discovery.DirectorySelector;
Expand Down Expand Up @@ -63,6 +64,14 @@ private static void recursivelyMerge(TestDescriptor descriptor, TestDescriptor p
);
}

void resolveClass(ClassSelector classSelector) {
Class<?> javaClass = classSelector.getJavaClass();
Cucumber annotation = javaClass.getAnnotation(Cucumber.class);
if (annotation != null) {
resolvePackageResource(javaClass.getPackage().getName());
}
}

void resolveDirectory(DirectorySelector selector) {
resolvePath(selector.getPath());
}
Expand All @@ -84,7 +93,10 @@ void resolveFile(FileSelector selector) {
}

void resolvePackageResource(PackageSelector selector) {
String packageName = selector.getPackageName();
resolvePackageResource(selector.getPackageName());
}

private void resolvePackageResource(String packageName) {
featureScanner
.scanForResourcesInPackage(packageName, packageFilter)
.stream()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static java.util.stream.Collectors.toSet;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClass;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathResource;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectClasspathRoots;
import static org.junit.platform.engine.discovery.DiscoverySelectors.selectDirectory;
Expand Down Expand Up @@ -210,6 +211,14 @@ void resolveRequestWithMultipleUniqueIdSelector() {
);
}

@Test
void resolveRequestWithClassSelector() {
DiscoverySelector resource = selectClass(RunCucumberTest.class);
EngineDiscoveryRequest discoveryRequest = new SelectorRequest(resource);
resolver.resolveSelectors(discoveryRequest, testDescriptor);
assertEquals(2, testDescriptor.getChildren().size());
}

private Optional<UniqueId> selectSomePickle(DiscoverySelector resource) {
EngineDiscoveryRequest discoveryRequest = new SelectorRequest(resource);
resolver.resolveSelectors(discoveryRequest, testDescriptor);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.cucumber.junit.platform.engine;

@Cucumber
public class RunCucumberTest {
}