Search code examples
cucumberbddcucumber-javacucumber-junit

Cucumber API for listing/run features/scenarios/tags


Are there some API (for Java lang) which can be used to:

  1. Retrieve the list of features files and all their scenarios
  2. Retrieve list of tags
  3. With information from 1 and 2, run a subset of these feature/scenarios either by name or by tag.

I intend to wrap these APIs with rest endpoints.

Later edit

pom.xml content

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-bom</artifactId>
            <version>7.16.1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>${version.junit}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <scope>provided</scope>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <version>5.4.0</version>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-java</artifactId>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-spring</artifactId>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit-platform-engine</artifactId>
    </dependency>
    <dependency>
        <groupId>io.cucumber</groupId>
        <artifactId>cucumber-junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
    </dependency>
    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
    </dependency>
</dependencies>

Solution

  • If you are using Cucumber in combination with the JUnit (5) Platform Engine, you can use the JUnit Launcher API to programmatically do all those things.

    For example listing scenarios and their tags:

    package io.cucumber.skeleton;
    
    import io.cucumber.junit.platform.engine.Constants;
    import org.junit.platform.engine.discovery.DiscoverySelectors;
    import org.junit.platform.launcher.Launcher;
    import org.junit.platform.launcher.TestIdentifier;
    import org.junit.platform.launcher.TestPlan;
    import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
    import org.junit.platform.launcher.core.LauncherFactory;
    
    public class LauncherDemo {
    
      public static void main(String[] args) {
    
        Launcher launcher = LauncherFactory.create();
    
    
        // Listing
        TestPlan testPlan = launcher.discover(
            LauncherDiscoveryRequestBuilder.request()
                .configurationParameter(Constants.JUNIT_PLATFORM_NAMING_STRATEGY_PROPERTY_NAME, "long")
                .selectors(DiscoverySelectors.selectPackage("io.cucumber.skeleton"))
                .build()
        );
        
        testPlan.getRoots().stream()
            .flatMap(root -> testPlan.getDescendants(root).stream())
            .filter(TestIdentifier::isTest)
            .forEach(testIdentifier -> {
              System.out.println(testIdentifier.getDisplayName() + " " + testIdentifier.getTags());
            });
    
    
        // Execution
        SummaryGeneratingListener summary = new SummaryGeneratingListener();
        launcher.registerTestExecutionListeners(summary);
    
        launcher.execute(
            LauncherDiscoveryRequestBuilder.request()
                .configurationParameter(Constants.JUNIT_PLATFORM_NAMING_STRATEGY_PROPERTY_NAME, "long")
                .selectors(DiscoverySelectors.selectPackage("io.cucumber.skeleton"))
                .filters(TagFilter.includeTags("ExampleTag"))
                .build()
        );
    
        summary.getSummary().printTo(new PrintWriter(System.out));
    
    
      }
    }