我有一个Cucumber-JVM,JUnit,Selenium设置。我通过在Eclipse中使用JUnit运行RunSmokeTests.java
来启动运行。我还设置了一个maven配置文件来从命令行运行测试,将来可能还有Jenkins。
当测试运行时,其中一些可能会失败,主要是由于应用程序花费的时间超过预期。然后我必须重新运行这些场景。目前我通过手动将@rerun
标记附加到失败的标记然后运行RunReruns.java
来运行它们,这类似于RunSmokeTest.java
但带有@rerun
标记。
随着自动化测试数量的增加,标记测试并开始运行并清除标记非常耗时。有没有使用Cucumber-JVM重新运行失败测试的自动化方法?
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>
答案 0 :(得分:5)
我提出了另一种解决方案,使用maven&amp; amp;重新运行失败的测试黄瓜。
RunNotifier
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);
}
}
这将运行测试,并且每当出现故障时,将失败的类输出到文件。诀窍是保持功能简短并创建大量测试类以避免重复测试。
@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 {
}
Rerun
个人资料。这有三件事:1)它将失败的类加载到内存中,2)清除失败的类属性文件,以及3)仅重新运行从属性文件加载的失败测试:
<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>
首次试运行:
mvn clean test
接下来的测试运行:
mvn clean test -Pretry
mvn clean test -Pretry
mvn clean test -Pretry
...
您可以根据需要重复多次,直到没有错误。
答案 1 :(得分:4)
我手边没有可执行的示例,但您也可以在jvm上执行此操作。有RerunFormatter
写入文本文件,列出失败方案的文件和行号:
@CucumberOptions(format = {"rerun:target/rerun.txt"})
您应该能够将此文件指定为另一个测试类的输入,方法是在其前面添加@
:
@CucumberOptions(features = {"@target/rerun.txt"})
答案 2 :(得分:1)
您可以按以下方式将黄瓜选项传递给mvn
mvn clean verify -Dcucumber.options="@rerun.txt"
请注意,这里有一个棘手的部分。如果您在首次运行和重新运行时都使用相同的测试运行器(我相信这就是您想要的),那么测试运行器将包含类似
的内容@CucumberOptions(plugin = { "rerun:target/rerun.txt"})
如果您使用与下面相同的重新运行文件名来使用maven进行重新运行
mvn clean verify -Dcucumber.options="@target/rerun.txt"
然后,黄瓜将抱怨找不到重新运行的文件。为什么?因为插件“ rerun:target / rerun.txt”将首先使用此测试运行程序删除文件。
解决方法是先复制/重命名文件,然后像这样启动mvn运行
mv target/rerun.txt rerun.txt && mvn clean verify -Dcucumber.options="@rerun.txt"
这实际上是您想要的。因为说文件target / rerun.txt中是否有5种失败的方案。经过一些修复后重新运行,其中两个通过了。现在,target / rerun.txt仅包含其余3个失败的方案,这将是您调试过程中的新起点。
答案 3 :(得分:0)
您可以使用cucumber-jvm-parallel-plugin提供的代码作为解决方法,直到它生效。点击命令如下所示。