使用cucumber-jvm重新尝试失败的黄瓜测试 [英] Rerunning failed cucumber tests using cucumber-jvm
问题描述
我有一个Cucumber-JVM,JUnit,Selenium设置。我通过在Eclipse中使用JUnit运行 RunSmokeTests.java
来启动运行。我还设置了一个maven配置文件来运行命令行的测试,以及将来可能的Jenkins。
I have a Cucumber-JVM, JUnit, Selenium setup. I initiate the run by running RunSmokeTests.java
using JUnit within Eclipse. I have also set up a maven profile to run the tests from command line, and possibly Jenkins in the future.
当测试运行时,其中一些可能会失败,主要是由于申请时间超过预期。然后我必须重新运行这些场景。目前我通过手动将 @rerun
标签附加到失败的标签然后运行 RunReruns.java
来运行它们,类似于 RunSmokeTest.java
,但 @rerun
标记。
When the tests are run then some of them may fail sometimes, mainly due to the application taking longer than expected. I would then have to re-run these scenarios. At the moment I run them by manually attaching @rerun
tag to the ones that failed and then running RunReruns.java
, which is similar to RunSmokeTest.java
but with @rerun
tag.
随着自动化测试数量的增加,标记测试并开始运行并清除标签非常耗时。是否有自动方式使用Cucumber-JVM重新运行失败的测试?
With the increasing number of automated tests it is time consuming to tag the tests and start the run and clear the tags. Is there a automated way with Cucumber-JVM to re-run failed tests?
RunSmokeTests.java
package testGlueClasses;
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
@RunWith(Cucumber.class)
@Cucumber.Options(features = "src/test/java", strict = true, format = {
"html:target/CucumberReport", "json:target/JSON/Cucumber.json",
"FrameworkCore.CustomTestReporter" }, tags = { "@SmokeTest" }, glue = {
"FrameworkCore", "MyApp.Utils", "MyApp.StepDefinitions" })
public class RunSmokeTests {
}
Maven代码段
<profile>
<id>smoke</id>
<properties>
<include.tests>
**/RunSmokeTests.java
</include.tests>
</properties>
</profile>
推荐答案
我想出了另一个重新运行失败测试的解决方案使用maven&黄瓜。
I came up with another solution to rerun just failed test using maven & cucumber.
public class RerunningCucumber extends Cucumber {
private final String className;
@SuppressWarnings("rawtypes")
public RerunningCucumber(Class clazz) throws InitializationError, IOException {
super(clazz);
className = clazz.getSimpleName();
}
@Override
public void run(RunNotifier notifier) {
notifier.addListener(new RunListener(){
public void testFailure(Failure failure) throws Exception {
Throwable error = failure.getException();
if (error instanceof AssertionError){
//Nothing. This is a normal failure. Continue
return;
}
//No! A wild exception has appeared!
//Let's run this test again.
RerunningCucumber.addFile(className);
}
});
super.run(notifier);
}
private static final String filename = "target/rerun.properties";
private static final Set<String> addedClasses = new HashSet<String>();
public static synchronized void addFile(String className) throws IOException{
//First find the file
if (addedClasses.contains(className)){
return;
}
File file = new File(filename);
if (!file.exists()){
//Need to create the file
PrintWriter writer = new PrintWriter(file, "UTF-8");
writer.print("retryclasses=**/"+className+".class");
writer.close();
}
else {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
out.print(",**/"+className+".class");
out.close();
}
addedClasses.add(className);
}
}
2)使用自定义类作为黄瓜的跑步者测试。
这将运行测试,并且每当发生故障时,将失败的类输出到文件。诀窍是保持功能简短并创建大量测试类以避免重复测试。
2) Use custom class as a runner for the cucumber tests.
This will run the tests, and whenever there is a failure, output the failed class to a file. Trick is to keep features short and create a lot of test classes to avoid repeating tests.
@RunWith(RerunningCucumber.class)
@CucumberOptions(features = {"classpath:features/testFeature.feature}, format = {
"html:target/cucumber-html-report/testFeature.html",
"json:target/cucumber-json-report/testFeature.json"},
tags = {"@testFeature"})
public class RunTestFeature {
}
3)向maven添加重新运行
个人资料。
这有三件事:1)它将失败的类加载到内存中,2)清除失败的类属性文件,3)仅重新运行从属性文件加载的失败的测试: / p>
3) Add a Rerun
profile to maven.
This does three things: 1) it loads the failed classes into memory, 2) cleans JUST the failed classes properties file, and 3) reruns ONLY the failed tests as loaded from the properties file:
<profile>
<id>retry</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>properties-maven-plugin</artifactId>
<version>1.0-alpha-2</version>
<executions>
<!-- Associate the read-project-properties goal with the initialize
phase, to read the properties file. -->
<execution>
<phase>pre-clean</phase>
<goals>
<goal>read-project-properties</goal>
</goals>
<configuration>
<files>
<file>target/rerun.properties</file>
</files>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>2.6.1</version>
<configuration>
<filesets>
<fileset>
<directory>target</directory>
<includes>
<include>rerun.properties</include>
</includes>
</fileset>
</filesets>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
<version>1.6</version>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>run</goal>
</goals>
<configuration>
<target>
<echo>Retrying the following classes: "${retryclasses}"</echo>
</target>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<includes>
<include>${retryclasses}</include>
</includes>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
4)用法
首次试运行:
4) Usage
First test run:
mvn clean test
下一个测试运行:
mvn clean test -Pretry
mvn clean test -Pretry
mvn clean test -Pretry
...
你可以重复多次如你所愿,直到没有错误。
You can repeat as many times as you want until there are no errors.
这篇关于使用cucumber-jvm重新尝试失败的黄瓜测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!