TestNG监听

这是我的TestNG教程中非常重要和最后一章。我花了很长时间才为我的观众找出一个完美的简单例子,因为我来自非技术背景,我围绕这么多网站来获得听众的完美例子,但我无法找到一个。每个人都在互联网上粘贴了几乎相同的例子。说够了,让我们现在开始吧。

在非技术术语中,TestNG通过套件,测试和方法管理所有内容,并且监听器使我们能够在每个套件,测试和方法之前和之后采取行动。

 

TestNG监听

TestNG中有许多类型的侦听器,例如IAnnotationTransformer,IAnnotationTransformer2,IConfigurable,IConfigurationListener,IConfigurationListener2,IExecutionListener,IHookable,IInvokedMethodListener,IInvokedMethodListener2,IMethodInterceptor,IReporter,ISuiteListener,ITestListener。

就测试问题而言,只有少数可以有效使用,例如:

ISuiteListener:它有两个方法onStart()onFinish()。每当类实现此侦听器时,TestNG都会保证最终用户在运行TestNG Suite之前和之后调用onStart()和onFinish()方法。因此,在TestNG选择执行套件之前,它首先调用onStart()方法并运行此方法中已编写脚本的任何内容。以类似的方式,它在套件运行后再次调用onFinish()方法。

ITestListener:此侦听器的工作方式与ISuiteListerner完全相同,但唯一的区别是它在Test之前和之后进行调用而不是套件。它有七种方法。

onFinish():在所有测试运行并调用所有配置方法后调用。

onStart(): 在实例化测试类之后和调用任何配置方法之前调用。

onTestFailedButWithinSuccessPercentage(ITestResult result): 每次方法失败时调用但已使用successPercentage注释,此失败仍将其保持在请求的成功百分比范围内。

onTestFailure(ITestResult结果):每次测试失败时调用。

onTestSkipped(ITestResult result): 每次跳过测试时调用

onTestStart(ITestResult result): 每次调用测试之前调用。

onTestSuccess(ITestResult result): 每次测试成功时调用。

 

IInvokedMethodListener:此侦听器的工作方式与ISuiteListerner和ITestListerner完全相同,唯一的区别是它在每个方法之前和之后进行调用。它只有两种方法。

afterInvocattion():在每个方法之后调用

beforeInvocation():在每个方法之前调用

好的,现在我猜你必须非常清楚听众是什么,我们也看到了如何编写一个监听器。现在是个大问题。

怎么做…

1)创建一个“ New Class ”文件,并为其命名为“ Listener ”,右键单击Package并选择New > Class

2)现在为这个新创建的类实现ISuiteListener,ITestListener和IInvokedMethodListener。为了实现一个监听器类,该类必须实现org.testng.ITestListener 接口。当测试开始,结束,失败,跳过或通过时,TestNG会在运行时通知这些类。

 

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

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

package utility;

 

import org.testng.IInvokedMethod;

 

import org.testng.IInvokedMethodListener;

 

import org.testng.ISuite;

 

import org.testng.ISuiteListener;

 

import org.testng.ITestContext;

 

import org.testng.ITestListener;

 

import org.testng.ITestNGMethod;

 

import org.testng.ITestResult;

 

import org.testng.Reporter;

 

public class Listener implements ITestListener, ISuiteListener, IInvokedMethodListener {

 

// This belongs to ISuiteListener and will execute before the Suite start

 

@Override

 

public void onStart(ISuite arg0) {

 

Reporter.log("About to begin executing Suite " + arg0.getName(), true);

 

}

 

// This belongs to ISuiteListener and will execute, once the Suite is finished

 

@Override

 

public void onFinish(ISuite arg0) {

 

Reporter.log("About to end executing Suite " + arg0.getName(), true);

 

}

 

// This belongs to ITestListener and will execute before starting of Test set/batch

 

public void onStart(ITestContext arg0) {

 

Reporter.log("About to begin executing Test " + arg0.getName(), true);

 

}

 

// This belongs to ITestListener and will execute, once the Test set/batch is finished

 

public void onFinish(ITestContext arg0) {

 

Reporter.log("Completed executing test " + arg0.getName(), true);

 

}

 

// This belongs to ITestListener and will execute only when the test is pass

 

public void onTestSuccess(ITestResult arg0) {

 

// This is calling the printTestResults method

 

printTestResults(arg0);

 

}

 

// This belongs to ITestListener and will execute only on the event of fail test

 

public void onTestFailure(ITestResult arg0) {

 

// This is calling the printTestResults method

 

printTestResults(arg0);

 

}

 

// This belongs to ITestListener and will execute before the main test start (@Test)

 

public void onTestStart(ITestResult arg0) {

 

System.out.println("The execution of the main test starts now");

 

}

 

// This belongs to ITestListener and will execute only if any of the main test(@Test) get skipped

 

public void onTestSkipped(ITestResult arg0) {

 

printTestResults(arg0);

 

}

 

public void onTestFailedButWithinSuccessPercentage(ITestResult arg0) {

 

}

 

// This is the method which will be executed in case of test pass or fail

 

// This will provide the information on the test

 

private void printTestResults(ITestResult result) {

 

Reporter.log("Test Method resides in " + result.getTestClass().getName(), true);

 

if (result.getParameters().length != 0) {

 

String params = null;

 

for (Object parameter : result.getParameters()) {

 

params += parameter.toString() + ",";

 

}

 

Reporter.log("Test Method had the following parameters : " + params, true);

 

}

 

String status = null;

 

switch (result.getStatus()) {

 

case ITestResult.SUCCESS:

 

status = "Pass";

 

break;

 

case ITestResult.FAILURE:

 

status = "Failed";

 

break;

 

case ITestResult.SKIP:

 

status = "Skipped";

 

}

 

Reporter.log("Test Status: " + status, true);

 

}

 

// This belongs to IInvokedMethodListener and will execute before every method including @Before @After @Test

 

public void beforeInvocation(IInvokedMethod arg0, ITestResult arg1) {

 

String textMsg = "About to begin executing following method : " + returnMethodName(arg0.getTestMethod());

 

Reporter.log(textMsg, true);

 

}

 

// This belongs to IInvokedMethodListener and will execute after every method including @Before @After @Test

 

public void afterInvocation(IInvokedMethod arg0, ITestResult arg1) {

 

String textMsg = "Completed executing following method : " + returnMethodName(arg0.getTestMethod());

 

Reporter.log(textMsg, true);

 

}

 

// This will return method names to the calling function

 

private String returnMethodName(ITestNGMethod method) {

 

return method.getRealClass().getSimpleName() + "." + method.getMethodName();

 

}

 

}

3)创建一个' New Class '  文件,并为其命名为' TestListener ',右键单击Package并选择  New  >  Class

 

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

package automationFramework;

 

import org.testng.annotations.AfterMethod;

 

import org.testng.annotations.BeforeMethod;

 

import org.testng.annotations.Test;

 

public class TestListener {

 

  @Test

 

  public void main() {

 

  System.out.println("Execution of Main test is carring on");

 

  }

 

  @BeforeMethod

 

  public void beforeMethod() {

 

  System.out.println("Execution of Before method is carring on");

 

  }

 

  @AfterMethod

 

  public void afterMethod() {

 

  System.out.println("Execution of After method is carring on");

 

  }

 

}

 

我如何让TestNG知道我有一个它应该在执行我的测试时调用的监听器?

基本上有两种方法可以将侦听器添加到特定类中。

1)为您的测试类实现TestNG Listener

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package automationFramework;

 

import org.testng.annotations.AfterMethod;

 

import org.testng.annotations.BeforeMethod;

 

import org.testng.annotations.Test;

 

// This code will implement TestNG listeners

 

@Listeners(PackageName.ListenerClassName)

 

// For e.g. @Listeners(utility.Listener.class)

 

public class TestListener {

 

  @Test

 

  public void main() {

 

  }

 

}

 

2)TestNG xml中的监听器标签:虽然方法1足以让你入门,但它不是一种“优雅”的使用监听器的方式,因为你被迫将这个@Listeners部分添加到你的每个类中不会想要的。所以你要做的就是创建一个TestNG Suite xml,然后将listeners部分添加到这个套件的xml文件中。这样,您的所有测试都将基本上利用您编写的侦听器。

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<suite name="Suite-Listeners" parallel="none">

 

<listeners>

 

<listener class-name="utility.Listener"></listener>

 

</listeners>

 

<test name="Batch-Listeners">

 

<classes>

 

<class name="automationFramework.TestListener" />

 

</classes>

 

</test>

 

</suite>

 

测试用例的输出如下所示:

TestNG听众

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值