简体   繁体   中英

How to dynamically set cucumberOptions?

I am trying to dynamically set the tags and the report locations so that I only need one testRunner. Note both are written in groovy, and I can only use up to JUnit 4.8 due to project constraints. Thank you for any help in advance:)

//CucumberTestRunner
    package cucumber
    import io.cucumber.junit.Cucumber
    import io.cucumber.junit.CucumberOptions
    import org.junit.runner.RunWith
    
    @RunWith(Cucumber.class)
    @CucumberOptions(
            features = "src/main/groovy/customFramework/dummyTests/cucumberTests/addDeal/",
            glue = "src/main/groovy/customFramework/testsPackage/cucumber/StepDefinitions/",
            publish = false,
            monochrome = true,
            tags = "${CucumberTestMaster.tag}",
            plugin = ["pretty", "junit:target/JUNITReports/report.xml", "html:target/HTMLReports/report.html",
                    "json:target/JSONReports/report.json"]
    )
    class CucumberTestRunner {
    }

//CucumberTestMasster

    package cucumber

class CucumberTestMaster {
    String tag="@Daily"
    static void main(String[] args) {
        new CucumberTestRunner()
    }
}

Unfortunately what you're trying to do is not possible with JUnit 4. You can however use Cucumbers main method. The example below use the maven-antrun-plugin , but any other way to run a main method works.

Note that the main method is a CLI application, so if you pass --help as an argument it will explain what it's options are.

https://github.com/cucumber/cucumber-jvm/blob/main/examples/calculator-java-cli/pom.xml#L53

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-antrun-plugin</artifactId>
                <executions>
                    <execution>
                        <id>cli-test</id>
                        <phase>test</phase>
                        <goals>
                            <goal>run</goal>
                        </goals>
                        <configuration>
                            <target unless="maven.test.skip">
                                <echo message="Running Cucumber through the CLI" />
                                <java classname="io.cucumber.core.cli.Main" fork="true" failonerror="true" newenvironment="true" maxmemory="512m" classpathref="maven.test.classpath">
                                    <arg value="--plugin" />
                                    <arg value="pretty" />
                                    <arg value="--plugin" />
                                    <arg value="html:target/results.html" />
                                    <arg value="--plugin" />
                                    <arg value="message:target/results.ndjson" />
                                </java>
                            </target>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

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