Feature files discovery in cucumber-junit-platform-engine
Asked Answered
S

2

8

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 i.e. 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.

Snorkel answered 4/11, 2020 at 8:35 Comment(0)
D
9

In cucumber-jvm v7 the @Cucumber annotation is deprecated and you're encouraged to use the regular @Suite annotation. 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)

Purpose of annotations:
@Suite - annotation from JUnit 5 to make this class a run configuration for test suite.
@IncludeEngines("cucumber") - tells JUnit 5 to use Cucumber test engine to run features.
@SelectClasspathResource("features") - to change the location of your feature files (if you do not add this annotation classpath of the current class will be used).
@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "com.mycompany.cucumber") - this annotation specifies the path to steps definitions (java classes).

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

Damiano answered 19/11, 2021 at 9:3 Comment(1)
What about in cucumber-jvm 6.9.1?Courlan
P
2

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)
}
Purr answered 4/11, 2020 at 15:53 Comment(7)
I made it for now by using matching path to features and source code but I will try to test it later, thx.Snorkel
@m-p-korstanje is there any rationale why features has been removed from properties? Would it be ok to request such functionality in cucumber-junit-platform-engine?Snorkel
Test discovery is a feature provided by the JUnit Platform. As such the Cucumber integration with the JUnit Platform can not provide it. You'd replace cucumber.features with the appropriate selector. junit.org/junit5/docs/current/user-guide/#launcher-apiPurr
I'm still not convinced, as per docsCucumber will scan the package of a class annotated with @Cucumber for feature files. So why can't this behaviour be overriden by features property to scan not the package of the runner but custom user provided package? I have many runners which scan all resources, i.e. features = "." and then filter the tests by tags.Snorkel
As I wrote in the docs, the annotation is a workaround for the lack of support from Gradle and Maven. I reckon that once supported the annotation will be deprecated. Consider using a custom Gradle job with the JUnit ConsoleLauncher.Purr
You may also benefit from the ability to run JUnit5 Suites programmatically once github.com/junit-team/junit5/pull/2416 is merged.Purr
the amount of work in the PR is absolutely staggering!Ingaborg

© 2022 - 2024 — McMap. All rights reserved.