TestNG - 运行失败的test

testNG 重试失败的case有两种方式:

1、所有case全部执行完后,主动触发执行所有失败的case

2、case失败后立即重试

1、运行所有的失败的case

testNG运行测试用例的时候会创建一个testng-failed.xml文件,这个文件里面包含了所有的失败的测试用例。触发执行testng-failed.xml文件

可以使用Maven命令执行此文件 mvn clean -Dxml.file=XX/testng-failed.xml test

2、自动重试失败的测试用例

//1.实现IRetryAnalyzer方法
import org.testng.IRetryAnalyzer;
import org.testng.ITestResult;
 
public class MyRetry implements IRetryAnalyzer {
 
  private int retryCount = 0;
  private static final int maxRetryCount = 3;
 
  @Override
  public boolean retry(ITestResult result) {
    if (retryCount < maxRetryCount) {
      retryCount++;
      return true;
    }
    return false;
  }
}


//2.在测试用例上添加注解
import org.testng.Assert;
import org.testng.annotations.Test;
 
public class TestclassSample {
 
  @Test(retryAnalyzer = MyRetry.class)
  public void test2() {
    Assert.fail();
  }
}

每次都在测试用例上添加注解太麻烦了,我们可以实现IAnnotationTransformer方法,可以修改@Test注解的属性

public class MyTransformer implements IAnnotationTransformer {
  public void transform(ITest annotation, Class testClass,
      Constructor testConstructor, Method testMethod)
  {
     annotation.setRetryAnalyzer(MyRetry.class);
  }
}

note:IAnnotationTransformer只能在 testng.xml 添加,因为TestNG要实施修改注释的操作

3、Maven+testNG 执行失败的case

在pom中进行入下配置,执行命令mvn test -Dxml.file=test-output/testng-failed.xml 就可以重试失败的case了

注意:

1、testng.xml不需要改成对应的保存失败用例的文件名称,只是定义了一个变量,因此执行不同的测试套件时不需要修改pom.xml文件

2、-D表示properties属性,根据后面的变量名可以覆盖pom文件中设置的值,变量名定义的是xml.file所以这里就需要写成-Dxml.file,如果变量名定义的是suiteXmlFile,那么命令就变成了mvn clean test -DsuiteXmlFile=test-output/testng-failed.xml 

……
<properties>

        <xml.file>testng.xml</suiteXmlFile>

</properties>
……
<plugin>

        <groupId>org.apache.maven.plugins</groupId>

        <artifactId>maven-surefire-plugin</artifactId>

         <version>2.16</version>

                <configuration>

                    <argLine>-Dfile.encoding=UTF-8</argLine>

                    <suiteXmlFiles>

                        <suiteXmlFile>${xml.file}</suiteXmlFile>

                    </suiteXmlFiles>

                </configuration>

 </plugin>
……

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值