在TestNG Framework中实现IRetryAnalyzer重试失败测试

在我们之前在TestNG重试失败测试的帖子中,我们讨论了IRetryAnalyzer接口以及我们可以为测试指定Retry Analyzer的各种方法。在这篇文章中,我们将以 最具设计意识的方式研究如何 在TestNG框架实现IRetryAnalyzer重试失败测试,以便我们可以进行可维护且易于管理的测试。这篇文章将要求您对Java注释有一个很好的理解。我们将创建一个自定义注释,我们将在@Test方法上使用它,以便我们可以直接在Test中指定重试次数

假设你已经完成了链接的帖子。有必要了解这里写的是什么。

 

1)创建自定义Java注释的步骤

我们要做的第一件事是创建一个Java注释,我们将注释我们的测试方法。假设注释的名称是RetryCountIfFailed。 此注释是纯Java注释。这是执行此操作的代码

 

1

2

3

4

5

6

7

8

9

10

11

12

13

package CustomAnnotations;

 

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

 

@Retention(RetentionPolicy.RUNTIME)

public @interface RetryCountIfFailed  {

 

// Specify how many times you want to

// retry the test if failed.

// Default value of retry count is 0

int value() default 0;

}

你可以看到,只有一个变量“值”里面RetryCountIfFailed  注解。此值指定在发生故障时需要重新执行测试的次数。请注意,默认值设置为0.此外,需要注意的一个关键点是它的运行时注释。因此我们已经指定

 

1

@Retention(RetentionPolicy.RUNTIME)

 

2)在TestNG自动化测试中使用自定义创建的Java注释的步骤

您可以使用注释来测试代码中的注释。这是使用此批注的更新测试代码。

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

package Tests;

 

import org.testng.Assert;

import org.testng.annotations.Test;

 

import CustomAnnotations.RetryCountIfFailed;

 

public class Test001 {

 

@Test

@RetryCountIfFailed(10)

public void Test1()

{

Assert.assertEquals(false, true);

}

 

@Test

public void Test2()

{

Assert.assertEquals(false, true);

}

}

注意RetryCountIfFailed注释的用法。首先,通过使用名称前面的@(@ RetryCountIfFailed),它就像任何其他注释一样使用。其次,将数值传递给注释,该注释指定需要重新运行失败测试的次数。因此,注释的实际用法变为@RetryCountIfFailed(5),其中5是您希望在发生故障时重新运行此测试的次数。该值可以是整数值。

 

3)在TestNG框架中实现IRetryAnalyzer重试失败测试

使用此注释在测试中使用它来更新IRetryAnalyzer接口的实现。一旦调用IRetryAnalyzer :: retry方法,我们就需要做两件事

  1. 检查调用重试的Test方法是否具有RetryCountIfFailed注释
  2. 然后将当前重试尝试与此批注的值进行比较。

这是RetryAnalyzer类的新实现

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

package Listeners;

 

import org.testng.IRetryAnalyzer;

import org.testng.ITestResult;

 

import CustomAnnotations.RetryCountIfFailed;

 

public class RetryAnalyzer implements IRetryAnalyzer {

 

int counter = 0;

/*

* (non-Javadoc)

*

* @see org.testng.IRetryAnalyzer#retry(org.testng.ITestResult)

*

* This method decides how many times a test needs to be rerun. TestNg will

* call this method every time a test fails. So we can put some code in here

* to decide when to rerun the test.

*

* Note: This method will return true if a tests needs to be retried and

* false it not.

*

*/

 

@Override

public boolean retry(ITestResult result) {

 

// check if the test method had RetryCountIfFailed annotation

RetryCountIfFailed annotation = result.getMethod().getConstructorOrMethod().getMethod()

.getAnnotation(RetryCountIfFailed.class);

// based on the value of annotation see if test needs to be rerun

if((annotation != null) && (counter < annotation.value()))

{

counter++;

return true;

}

return false;

}

}

这种方式根据RetryCountIfFailed注释中指定的计数进行重试决策。其余代码与前一篇文章中解释的相同。现在让我们看看Test1和Test2的运行次数。

在TestNG Framework中实现IRetryAnalyzer重试失败测试

在这里你可以看到Test1运行10次而Test2只运行一次。我希望我能够详细解释这个话题。您还可以在github上查看/克隆此项目。

 

了解如何在TestNG
https://github.com/virenv/TestngRetryCount 
5 forks中的失败测试用例上实现重试逻辑
2星。
0开放问题。
最近的提交:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值