Java 使用黄瓜 jvm 重新运行失败的黄瓜测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21334207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rerunning failed cucumber tests using cucumber-jvm
提问by shreyansp
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.
我有一个 Cucumber-JVM、JUnit、Selenium 设置。我通过RunSmokeTests.java
在 Eclipse 中使用 JUnit运行来启动运行。我还设置了一个 maven 配置文件来从命令行运行测试,将来可能还会运行 Jenkins。
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.
当测试运行时,其中一些有时可能会失败,主要是由于应用程序花费的时间比预期的要长。然后我将不得不重新运行这些场景。目前我通过手动将@rerun
标签附加到失败的然后运行它们来运行它们RunReruns.java
,这类似于RunSmokeTest.java
但带有@rerun
标签。
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?
随着自动化测试数量的增加,标记测试并开始运行并清除标记非常耗时。Cucumber-JVM 是否有自动重新运行失败测试的方法?
RunSmokeTests.java
运行SmokeTests.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 snippet:
Maven 片段:
<profile>
<id>smoke</id>
<properties>
<include.tests>
**/RunSmokeTests.java
</include.tests>
</properties>
</profile>
回答by J?rn Horstmann
I don't have an executable example at hand, but you can do this also on the jvm. There is a RerunFormatter
that writes a text file listing the file and line numbers of failed scenarios:
我手头没有可执行示例,但您也可以在 jvm 上执行此操作。有一个RerunFormatter
写一个文本文件,列出失败场景的文件和行号:
@CucumberOptions(format = {"rerun:target/rerun.txt"})
You should be able to specify this file as input for another test class by prefixing it with @
:
您应该能够通过添加前缀来将此文件指定为另一个测试类的输入@
:
@CucumberOptions(features = {"@target/rerun.txt"})
回答by Jason
I came up with another solution to rerun just failed test using maven & cucumber.
我想出了另一种解决方案,可以使用 maven & Cucumber 重新运行刚刚失败的测试。
1) Record test failures using a RunNotifier
1) 使用 a 记录测试失败 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);
}
}
2) Use custom class as a runner for the cucumber tests.
2) 使用自定义类作为黄瓜测试的运行器。
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) Add a Rerun
profile to maven.
3)Rerun
向 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:
这做了三件事: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>
4) Usage
4) 用法
First test run:
第一次试运行:
mvn clean test
Next test runs:
下一个测试运行:
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.
您可以根据需要重复多次,直到没有错误为止。
回答by Sugat Mankar
You can use cucumber-jvm-parallel-plugincontributed code as a workaround until it goes live. Hit commands as shown below.
您可以使用Cucumber-jvm-parallel-plugin贡献的代码作为解决方法,直到它上线。点击命令如下图所示。
- git clone -b tagwiseOutlinewiseIssueRerun https://github.com/sugatmankar/cucumber-jvm-parallel-plugin.git
- mvn clean install.
- Now edit you project pom file and use as stated here.
- Example for using this plugin is here.
- git clone -b tagwiseOutlinewiseIssueRerun https://github.com/sugatmankar/cucumber-jvm-parallel-plugin.git
- mvn 干净安装。
- 现在编辑您的项目 pom 文件并按照此处所述使用。
- 使用此插件的示例是here。
回答by Wenzhong Hu
You can pass cucumber options to mvn as below
您可以将黄瓜选项传递给 mvn,如下所示
mvn clean verify -Dcucumber.options="@rerun.txt"
Note there is a tricky part here. If you are using the same test runner for both first run and rerun (and I believe that's what you want), then the test runner would contains something like
请注意,这里有一个棘手的部分。如果您在第一次运行和重新运行时使用相同的测试运行器(我相信这就是您想要的),那么测试运行器将包含类似
@CucumberOptions(plugin = { "rerun:target/rerun.txt"})
If you fire your rerun with maven using the same rerun file name as below
如果您使用以下相同的重新运行文件名使用 maven 重新运行
mvn clean verify -Dcucumber.options="@target/rerun.txt"
then cucumber will complain it could not find the rerun file. Why? Because the plugin "rerun:target/rerun.txt" will delete the file first with this test runner.
然后黄瓜会抱怨它找不到重新运行的文件。为什么?因为插件“rerun:target/rerun.txt”会先用这个test runner删除文件。
Workaround is copy/rename the file first, then kick off the mvn run like
解决方法是先复制/重命名文件,然后像这样启动 mvn 运行
mv target/rerun.txt rerun.txt && mvn clean verify -Dcucumber.options="@rerun.txt"
And this is actually what you want. Because say if there are 5 failed scenarios in file target/rerun.txt. And with the rerun after some fix, 2 of them passed. Now the target/rerun.txt will contain the remaining 3 failed scenarios only, which would be your new start point along the debugging way.
而这实际上正是你想要的。因为假设文件 target/rerun.txt 中有 5 个失败的场景。经过一些修复后重新运行,其中 2 个通过了。现在 target/rerun.txt 将只包含剩余的 3 个失败场景,这将是您调试过程中的新起点。