使用Maven / Cucumber / Serenity重新运行失败的场景

时间:2018-03-06 13:52:04

标签: maven selenium-webdriver cucumber serenity-bdd cucumber-serenity

是否有人使用maven surefire插件或任何其他机制重新运行失败的方案。 我正在使用Cucumber with Serenity和Maven。我尝试了以下不同方式,以便在没有任何人工干预的情况下重新运行失败的情况 例如:如果5个测试用例中有2个测试用例出错,则自动我的脚本应该在生成最终的宁静报告之前执行这2个失败的方案 的 1。 Maven surefire
我在pom.xml文件中添加了以下行

<properties>
    <failsafe.rerunFailingTestsCount>2</failsafe.rerunFailingTestsCount>
    <surefire.rerunFailingTestsCount>2</surefire.rerunFailingTestsCount>
</properties>



        <plugin>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19.1</version>
            <configuration>
                <includes>
                    <include>**/*.java</include>
                </includes>
                <argLine>-Xmx512m</argLine>
                <systemPropertyVariables>
                    <webdriver.driver>${webdriver.driver}</webdriver.driver>                        
                </systemPropertyVariables>
                <systemProperties>
                    <webdriver.driver>${webdriver.driver}</webdriver.driver> 
                    <surefire.rerunFailingTestsCount>${surefire.rerunFailingTestsCount}</surefire.rerunFailingTestsCount>
                    <surefire.rerunFailingTestsCount>${surefire.rerunFailingTestsCount}</surefire.rerunFailingTestsCount>
                </systemProperties>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

然后我尝试执行下面的命令(我的场景由@RunThis标签注释)

mvn -Dcucumber.options =&#34; - 标签@ RunThis&#34; -Dfailsafe.rerunFailingTestsCount = 2 test

执行上述命令后,我希望失败的场景应该再次执行,但不会重新执行

2。使用黄瓜格式化程序生成rerun.txt并执行它
根据另一个解决方案,我使用黄瓜格式化程序生成rerun.txt。正在获取错误的场景将在rerun.txt中列出,然后应该由第二个跑步者类选择并执行那些失败的场景。
我能够成功生成rerun.txt但是第二个跑步者类既没有被执行也没有得到任何错误

Runnerclass 1

 @RunWith(CucumberWithSerenity.class)
    @CucumberOptions(
            features = "src/test/resources/features",
            tags = "@RunThis",
            monochrome = true
            ,plugin = { "json:target/cucumber-report-composite.json", "pretty",
                "html:target/cucumber/","rerun:target/rerun.txt" })
    public class AcceptanceTest {   

    }

RunnerClass 2

@RunWith(CucumberWithSerenity.class)
    @CucumberOptions(strict = true, 
      glue = { "src/test/resources/features" }, 
      features = { "@target/rerun.txt" }, 
      plugin = { "json:target/cucumber-report-composite.json", "pretty",
        "html:target/cucumber/"})
public class AcceptanceTest2 {

}

第3。使用@ExtendCucumberOptions
根据另一个解决方案,我们可以在下面添加对pom.xml文件的依赖性

<dependency>
    <groupId>com.github.mkolisnyk</groupId>
    <artifactId>cucumber-runner</artifactId>
    <version>1.3.1</version>
</dependency>

然后我们的跑步者类应该是这样的

@RunWith(CucumberWithSerenity.class)
@ExtendedCucumberOptions(jsonReport = "target/cucumber.json",
retryCount = 3,
jsonUsageReport = "target/cucumber-usage.json",
outputFolder = "target")
@CucumberOptions(
        features = "src/test/resources/features",
        tags = "@RunThis",
        monochrome = true
        ,plugin = { "json:target/cucumber-report-composite.json", "pretty",
                "html:target/cucumber/","rerun:target/rerun.txt" })
public class AcceptanceTest {


}

然后我在命令
下面执行 mvn -Dcucumber.options =&#34; - 标签@ RunThis&#34; 再次,我看不出失败的情况正在重新执行


完整的POM.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>GROUPNAME</groupId>
    <artifactId>ARTIFACTNAME</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Serenity project using Cucumber and WebDriver</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <failsafe.rerunFailingTestsCount>2</failsafe.rerunFailingTestsCount>
        <surefire.rerunFailingTestsCount>2</surefire.rerunFailingTestsCount>
        <serenity.version>1.6.3</serenity.version>
        <serenity.cucumber.version>1.5.9</serenity.cucumber.version>
        <serenity.maven.version>1.2.1</serenity.maven.version>
        <webdriver.driver>chrome</webdriver.driver>
    </properties>

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray</name>
            <url>http://jcenter.bintray.com</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray-plugins</name>
            <url>http://jcenter.bintray.com</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <!-- <dependency> <groupId>net.serenity-bdd.maven.plugins</groupId> <artifactId>serenity-maven-plugin</artifactId> 
            <version>1.5.0-rc.1</version> </dependency> -->

        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber</artifactId>
            <version>${serenity.cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.lambdaj</groupId>
            <artifactId>lambdaj</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20171018</version>
        </dependency>
        <dependency>
            <groupId>com.qpid.automation</groupId>
            <artifactId>AutomationToolKit</artifactId>
            <version>1.0.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.github.mkolisnyk</groupId>
            <artifactId>cucumber-runner</artifactId>
            <version>1.3.1</version>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <!-- <skip>true</skip> -->
                    <argLine>-Dfile.encoding="UTF-8"</argLine>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.19.1</version>
                <configuration>
                    <includes>
                        <include>**/*.java</include>
                    </includes>
                    <argLine>-Xmx512m</argLine>
                    <systemPropertyVariables>
                        <webdriver.driver>${webdriver.driver}</webdriver.driver>                        
                    </systemPropertyVariables>
                    <systemProperties>
                        <webdriver.driver>${webdriver.driver}</webdriver.driver> 
                        <surefire.rerunFailingTestsCount>${surefire.rerunFailingTestsCount}</surefire.rerunFailingTestsCount>
                        <surefire.rerunFailingTestsCount>${surefire.rerunFailingTestsCount}</surefire.rerunFailingTestsCount>
                    </systemProperties>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- Generate the test reports after the integration tests -->
            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

1 个答案:

答案 0 :(得分:1)

我可以使用以下代码重新运行。

@RunWith(CucumberWithSerenity.class)
@CucumberOptions(
        features = "src/test/resources/features", tags = "@Smoke1",
        monochrome = true,
        plugin = {"pretty", "html:target/cucumber-reports", "json:target/cucumber.json","rerun:rerun.txt"})
public class AcceptanceTest {

}


我刚刚执行以下命令,在上面的插件中添加了重新运行(@CucumberOptions)

mvn clean verify -Dcucumber.options="--tags @Smoke1"

并自动重新执行失败的方案。我不需要编写另一个运行类并手动运行它。

我的pom.xml是:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>GROUPID</groupId>
    <artifactId>ARTIFACTID</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>Serenity project using Cucumber and WebDriver</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <serenity.version>1.9.2</serenity.version>
        <serenity.cucumber.version>1.9.3</serenity.cucumber.version>
        <serenity.maven.version>1.8.21</serenity.maven.version>
        <webdriver.driver>chrome</webdriver.driver>
    </properties>

    <repositories>
        <repository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray</name>
            <url>http://jcenter.bintray.com</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
            <id>central</id>
            <name>bintray-plugins</name>
            <url>http://jcenter.bintray.com</url>
        </pluginRepository>
    </pluginRepositories>

    <dependencies>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-core</artifactId>
            <version>${serenity.version}</version>
        </dependency>
        <dependency>
            <groupId>net.serenity-bdd</groupId>
            <artifactId>serenity-cucumber</artifactId>
            <version>${serenity.cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.1.0</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>com.googlecode.lambdaj</groupId>
            <artifactId>lambdaj</artifactId>
            <version>2.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20171018</version>
        </dependency>
        <dependency>
            <groupId>com.qpid.automation</groupId>
            <artifactId>AutomationToolKit</artifactId>
            <version>1.0.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <!-- <skip>true</skip> -->
                    <argLine>-Dfile.encoding="UTF-8"</argLine>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <includes>
                        <include>**/*.java</include>
                    </includes>
                    <argLine>-Xmx512m</argLine>
                    <systemPropertyVariables>
                        <webdriver.driver>${webdriver.driver}</webdriver.driver>
                    </systemPropertyVariables>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <!-- Generate the test reports after the integration tests -->
            <plugin>
                <groupId>net.serenity-bdd.maven.plugins</groupId>
                <artifactId>serenity-maven-plugin</artifactId>
                <version>${serenity.version}</version>
                <executions>
                    <execution>
                        <id>serenity-reports</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>aggregate</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>