简体   繁体   中英

Feature files discovery in cucumber-junit-platform-engine

In cucumber-junit library I use @CucumberOptions to define feature files location:

package com.mycompany.cucumber;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;

@RunWith(Cucumber.class)
@CucumberOptions(
  plugin = ...,
  features = "classpath:.", // my java step definitions are in package com.mycompany.cucumber 
                            // but feature files directly in test resources 
                            // resources/is_it_friday_yet.feature
  tags = ...,
  glue = ...
)
public class CucumberRunner {
}

I'm running my tests with custom gradle task cucumberTest

cucumberTest {
    useJUnitPlatform()
}

After migrating to cucumber-junit-platform-engine @CucumberOptions are no longer supported.

package com.mycompany.cucumber;
import io.cucumber.junit.platform.engine.Cucumber;
@Cucumber
public class CucumberRunner {
}

I can make it work with replacing plugin , tags , glue options with properties cucumber.filter.tags , cucumber.glue , cucumber.plugin .

What about features property? It works fine if I change feature files location to match package name ie resources/com/mycompany/cucumber/is_it_friday_yet.feature . Still this is a simple case and I have many more test packages which are not placed in the same locations as source code and I cannot move them.

Gradle doesn't support non-class based test engines . However you can create a custom task that uses the JUnit Platform ConsoleLauncher . You can then use the Junit 5 selectors that are supported by Cucumber to select your features. For example the FileSelector with --select-file .

val consoleLauncherTest by tasks.creating(JavaExec::class) {
    dependsOn("testClasses")
    val reportsDir = file("$buildDir/test-results")
    outputs.dir(reportsDir)
    classpath(sourceSets["test"].runtimeClasspath)
    main = "org.junit.platform.console.ConsoleLauncher"
    args("--select-file", "path/to.feature")
    args("--details", "tree")
    args("--reports-dir", reportsDir)
}

In cucumber-jvm v7 the @Cucumber annotation is deprecated and you're encouraged to use the regular @Suite annotation. This means you can also use annotations like @SelectClasspathResource to change the location of your feature files. This works for me:

@Suite
@IncludeEngines("cucumber")
@SelectClasspathResource("features")
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.mycompany.cucumber")
public class CucumberIT {
}

It picks up all my .feature files under the features/ dir in my resources folder (classpath)

Docs: https://github.com/cucumber/cucumber-jvm/tree/main/junit-platform-engine#suites-with-different-configurations

There are various other @Select* annotations supported by junit-platform, I assume those work as well (though they're marked as experimental so subject to change): https://junit.org/junit5/docs/current/user-guide/#api-evolution-experimental-apis

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM