【问题标题】:Rerunning the failed scenario using Maven/Cucumber/Serenity使用 Maven/Cucumber/Serenity 重新运行失败的场景
【发布时间】:2018-03-06 13:52:03
【问题描述】:

有没有人使用 maven surefire 插件或任何其他机制来重新运行失败的场景。 我正在使用带有 Serenity 和 Maven 的 Cucumber。我尝试了以下不同的方法,以便在没有任何人工干预的情况下重新运行失败的场景
例如:如果 5 个测试用例中有 2 个测试用例出错,那么我的脚本应该在生成最终的宁静报告之前自动执行这 2 个失败的场景
1。 Maven 万无一失
我在 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="--tags @RunThis" -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>

那么我们的runner类应该是这样的

@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="--tags @RunThis"
我再次看不到失败的场景正在重新执行


完整的 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>

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

    • 两个问题:1. mvn clean verify -Dcucumber.options="--tags @Smoke1" 在这个命令中你提到运行失败的场景2.我的目标文件夹没有cucumber.json 文件,你从哪里得到的?请帮忙,我也需要这个修复。
    • @paul, Line '"rerun:rerun.txt"' 在插件中将负责重新运行失败的场景。
      工作原理 - 当你第一次时间执行您的场景,假设总共 10 个场景中有 2 个场景出错。这两个场景将在 rerun.txt 中记录下来。执行成功后,cucumber 将检查 rerun.txt,如果它包含任何场景,它将再次运行那些场景
    • @SachinB 请问如何在 Jenkins 中使用此功能?
    • @Python_Novice,无需对 Jenkins 作业进行任何更改,您将照常使用 Jenkins 执行您的套件。当任何场景出错时,相应的条目将被添加到“rerun.txt”中,在套件完全执行后,如果在“rerun.txt”中添加了任何条目,那么这个特定场景将被重新执行
    • @SachinB - 不知道那天我在想什么,但今天我非常清楚地理解了这个比喻。它就像一个魅力。谢谢你让我开心!
    猜你喜欢
    • 1970-01-01
    • 2017-08-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多