[英]Parallel Cucumber-JVM Selenium Grid Tests using Maven Surefire
[英]Rerunning failed cucumber tests using cucumber-jvm
我有一個 Cucumber-JVM、JUnit、Selenium 設置。 我通過在 Eclipse 中使用 JUnit 運行
RunSmokeTests.java<\/code>來啟動運行。
我還設置了一個 Maven 配置文件來從命令行運行測試,將來可能還有 Jenkins。
當測試運行時,其中一些有時可能會失敗,主要是由於應用程序花費的時間比預期的要長。 然后我將不得不重新運行這些場景。 目前,我通過手動將
@rerun<\/code>標簽附加到失敗的標簽然后運行
RunReruns.java<\/code>來運行它們,這類似於
RunSmokeTest.java<\/code>但帶有
@rerun<\/code>標簽。
隨着自動化測試數量的增加,標記測試並開始運行和清除標記非常耗時。 Cucumber-JVM 是否有一種自動化的方式來重新運行失敗的測試?
運行SmokeTests.java<\/strong>
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和黃瓜重新運行失敗的測試。
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
...
您可以根據需要重復多次,直到沒有錯誤。
我手邊沒有可執行的示例,但您也可以在jvm上執行此操作。 RerunFormatter
會寫一個文本文件,列出失敗方案的文件和行號:
@CucumberOptions(format = {"rerun:target/rerun.txt"})
您應該能夠通過在@
前面添加前綴來將此文件指定為另一個測試類的輸入:
@CucumberOptions(features = {"@target/rerun.txt"})
您可以將黃瓜選項傳遞給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個失敗的場景。 經過一些修復后重新運行,其中2個通過。 現在,target / rerun.txt將僅包含剩余的3個失敗方案,這將是調試方式的新起點。
您可以使用cucumber-jvm-parallel-plugin貢獻的代碼作為解決方法,直到它上線。 命中命令如下所示。
對於 maven 上的 cucumber + java,我發現了這個命令: mvn clean test -Dsurefire.rerunFailingTestsCount=2
,你必須有surefire插件的實際版本,我的是3.0.0-M5。 沒有什么特別的你甚至需要。 在這里找到解決方案Surefire 重新運行失敗的測試不起作用
1)<\/strong>使用 junit4 (cucumber-junit 引擎),它可以通過
rerun<\/code>插件和
features<\/code> cucumber 選項輕松完成。
為失敗的場景添加另一個 Maven 配置文件,例如 RerunCucumber.class。
使用主測試運行器運行您的初始構建並傳遞
rerun<\/code>插件:
@RunWith(Cucumber.class)
@CucumberOptions(tags = "@wip",
monochrome = true,
plugin = {"html:target/cucumber", "json:target/wip.json", "rerun:target/rerun_wip.txt"})
public class RunCucumber {
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.