使用cucumber-jvm重新尝试失败的黄瓜测试

 初初初初丶初崽崽__冏每_472 发布于 2023-01-31 16:01

我有一个Cucumber-JVM,JUnit,Selenium设置.我通过RunSmokeTests.java在Eclipse中使用JUnit来启动运行.我还设置了一个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片段:

    
        smoke
        
            
                **/RunSmokeTests.java
            
        
    

Jason.. 6

我提出了另一种解决方案,使用maven和黄瓜重新运行失败的测试.

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 addedClasses = new HashSet();
    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)使用自定义类作为黄瓜测试的跑步者.

这将运行测试,并且每当发生故障时,将失败的类输出到文件.诀窍是保持功能简短并创建大量测试类以避免重复测试.

@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)Rerun向maven 添加配置文件.

这样做有三件事:1)它将失败的类加载到内存中,2)清除失败的类属性文件,3)仅重新运行从属性文件加载的失败的测试:

    
        retry
        
            
                
                    org.codehaus.mojo
                    properties-maven-plugin
                    1.0-alpha-2
                    
                        
                        
                            pre-clean
                            
                                read-project-properties
                            
                            
                                
                                    target/rerun.properties
                                
                            
                        
                    
                
                
                    org.apache.maven.plugins
                    maven-clean-plugin
                    2.6.1
                    
                        
                            
                                target
                                
                                    rerun.properties
                                
                            
                        
                    
                
                
                    org.apache.maven.plugins
                    maven-antrun-plugin
                    1.6
                    
                        
                            compile
                            
                                run
                            
                            
                                
                                    Retrying the following classes: "${retryclasses}"
                                
                            
                        
                    
                
                
                    org.apache.maven.plugins
                    maven-surefire-plugin
                    2.17
                    
                        
                            ${retryclasses}
                        
                        true
                    
                    
                        
                            test
                            
                                test
                            
                        
                    
                
            
        
    

4)用法

首次试运行:

mvn clean test

下一个测试运行:

mvn clean test -Pretry
mvn clean test -Pretry
mvn clean test -Pretry
...

您可以根据需要重复多次,直到没有错误.

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有