-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmethod_invocations.html
521 lines (470 loc) · 20.7 KB
/
method_invocations.html
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="generator" content="Asciidoctor 2.0.18">
<title>Listening to TestNG lifecycle events</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Open+Sans:300,300italic,400,400italic,600,600italic%7CNoto+Serif:400,400italic,700,700italic%7CDroid+Sans+Mono:400,700">
<link rel="stylesheet" href="./asciidoctor.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/prettify.min.css">
</head>
<body class="article toc2 toc-left">
<div id="header">
<div id="toc" class="toc2">
<div id="toctitle">Table of Contents</div>
<ul class="sectlevel2">
<li><a href="#_listening_to_testng_lifecycle_events">Listening to TestNG lifecycle events</a></li>
<li><a href="#_listening_to_method_invocations">Listening to method invocations</a></li>
<li><a href="#_listening_to_configuration_invocations">Listening to configuration invocations</a></li>
<li><a href="#_listening_to_class_level_invocations">Listening to class level invocations</a></li>
<li><a href="#_listening_to_data_provider_invocations">Listening to data provider invocations</a></li>
<li><a href="#_listening_to_suite_level_invocations">Listening to Suite level invocations</a></li>
</ul>
</div>
</div>
<div id="content">
<div class="sect2">
<h3 id="_listening_to_testng_lifecycle_events">Listening to TestNG lifecycle events</h3>
<div class="paragraph">
<p>The listener {javadocs-base-url}/org/testng/IExecutionListener.html[IExecutionListener] allows you to be notified whenever TestNG is about to commence/conclude its execution. This listener can be used to perform setup and teardown activities at the test application layer itself ( for e.g., you could leverage this listener to boot up docker containers that are required for your application and shutdown them gracefully.)</p>
</div>
<div class="paragraph">
<p>Please note that this is the first listener TestNG would execute before it commences executing any of the <code><suite></code> found and also this would be the last listener to be invoked by TestNG (after the reporting listeners) before TestNG exits.</p>
</div>
<div class="paragraph">
<p>This listener should be declared, as explained in the section about <a href="testng_listeners.html">TestNG listeners</a>.</p>
</div>
<div class="paragraph">
<p>Here’s a sample listener implementation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.IExecutionListener;
public class SimpleExecutionListener implements IExecutionListener {
@Override
public void onExecutionStart() {
System.err.println("TestNG is commencing execution");
}
@Override
public void onExecutionFinish() {
System.err.println("TestNG is finished execution");
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here’s a sample test class that uses this above listener.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(SimpleExecutionListener.class)
public class SampleTestCase {
@Test
public void testMethod1() {}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The execution output would look like below:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="bash">SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
TestNG is commencing execution
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
TestNG is finished execution
Process finished with exit code 0</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_listening_to_method_invocations">Listening to method invocations</h3>
<div class="paragraph">
<p>The listener {javadocs-base-url}/org/testng/IInvokedMethodListener.html[IInvokedMethodListener] allows you to be notified whenever TestNG is about to invoke a test (annotated with <code>@Test</code>) or configuration (annotated with any of the <code>@Before</code> or <code>@After</code> annotation) method and declare it as a listener, as explained in the section about <a href="testng_listeners.html">TestNG listeners</a>.</p>
</div>
<div class="paragraph">
<p>Here’s a sample listener implementation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
public class SimpleInvokedMethodListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
log("Commencing", method);
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
log("Completed", method);
}
private static void log(String prefix, IInvokedMethod method) {
String type = "Configuration";
if (method.isTestMethod()) {
type = "Test";
}
String msg = prefix + " executing [" + type + "] method "
+ method.getTestMethod().getQualifiedName() + "()";
System.err.println(msg);
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here’s a sample test class that’s using this above listener.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(SimpleInvokedMethodListener.class)
public class SampleTestCase {
@BeforeMethod
public void setup() {}
@Test
public void testMethod1() {}
@Test
public void testMethod2() {}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here’s how the execution console would look like:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="bash">SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Commencing executing [Configuration] method org.testng.demo.configs.SampleTestCase.setup()
Completed executing [Configuration] method org.testng.demo.configs.SampleTestCase.setup()
Commencing executing [Test] method org.testng.demo.configs.SampleTestCase.testMethod1()
Completed executing [Test] method org.testng.demo.configs.SampleTestCase.testMethod1()
Commencing executing [Configuration] method org.testng.demo.configs.SampleTestCase.setup()
Completed executing [Configuration] method org.testng.demo.configs.SampleTestCase.setup()
Commencing executing [Test] method org.testng.demo.configs.SampleTestCase.testMethod2()
Completed executing [Test] method org.testng.demo.configs.SampleTestCase.testMethod2()
===============================================
Default Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_listening_to_configuration_invocations">Listening to configuration invocations</h3>
<div class="paragraph">
<p>The listener {javadocs-base-url}/org/testng/IConfigurationListener.html[IConfigurationListener] allows you to be notified whenever TestNG is about to invoke a configuration (annotated with any of the <code>@Before</code> or <code>@After</code> annotation) method and declare it as a listener, as explained in the section about <a href="testng_listeners.html">TestNG listeners</a>. This listener also lets you be notified about whether a configuration passed, failed (or) if it got skipped.</p>
</div>
<div class="paragraph">
<p>Here’s a sample listener implementation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.IConfigurationListener;
import org.testng.ITestNGMethod;
import org.testng.ITestResult;
public class MyConfigurationListener implements IConfigurationListener {
@Override
public void beforeConfiguration(ITestResult tr, ITestNGMethod tm) {
//The "ITestNGMethod" will be a valid object ONLY for @BeforeMethod and @AfterMethod
System.err.println("Commencing execution of Config method " + tr.getMethod().getQualifiedName() +
" for the test method " + tm.getQualifiedName());
}
@Override
public void onConfigurationSuccess(ITestResult tr, ITestNGMethod tm) {
//The "ITestNGMethod" will be a valid object ONLY for @BeforeMethod and @AfterMethod
System.err.println("Successfully executed Config method " + tr.getMethod().getQualifiedName() +
" for the test method " + tm.getQualifiedName());
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here’s a sample test class that’s using this above listener.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(MyConfigurationListener.class)
public class SampleTestClass {
@BeforeMethod
public void beforeMethodConfig() {
System.err.println("Executing config method beforeMethodConfig()");
}
@Test
public void testMethod() {
System.err.println("Executing test method");
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here’s how the execution console would look like:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="bash">SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Commencing execution of Config method org.testng.demo.SampleTestClass.beforeClass for the test method org.testng.demo.SampleTestClass.testMethod
Successfully executed Config method org.testng.demo.SampleTestClass.beforeClass for the test method org.testng.demo.SampleTestClass.testMethod
===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_listening_to_class_level_invocations">Listening to class level invocations</h3>
<div class="paragraph">
<p>The listener {javadocs-base-url}/org/testng/IClassListener.html[IClassListener] allows you to be notified whenever TestNG is about to start processing a test class and invoke its configuration/tests.</p>
</div>
<div class="paragraph">
<p>Add the listener implementation, as explained in the section about <a href="testng_listeners.html">TestNG listeners</a>.</p>
</div>
<div class="paragraph">
<p>Here’s a sample listener implementation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.IClassListener;
import org.testng.ITestClass;
public class ClassLevelListener implements IClassListener {
@Override
public void onBeforeClass(ITestClass testClass) {
System.err.println("Commencing execution for the test class : " + testClass.getRealClass().getName());
}
@Override
public void onAfterClass(ITestClass testClass) {
System.err.println("Completed execution for the test class : " + testClass.getRealClass().getName());
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Here’s a sample test class that consumes the above shown sample listener.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(ClassLevelListener.class)
public class SampleTestCase {
@BeforeMethod
public void setup() {}
@Test
public void testMethod1() {}
@Test
public void testMethod2() {}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>Execution output would be as below:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="bash">SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Commencing execution for the test class : org.testng.demo.configs.SampleTestCase
Completed execution for the test class : org.testng.demo.configs.SampleTestCase
===============================================
Default Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_listening_to_data_provider_invocations">Listening to data provider invocations</h3>
<div class="paragraph">
<p>The listener {javadocs-base-url}/org/testng/IDataProviderListener.html[IDataProviderListener] allows you to be notified whenever TestNG is about invoke data provider methods.
Refer to <a href="parameters.html#_parameters_with_dataproviders">here</a> to learn how to work with data driven tests.</p>
</div>
<div class="paragraph">
<p>Add the listener implementation, as explained in the section about <a href="testng_listeners.html">TestNG listeners</a>.</p>
</div>
<div class="paragraph">
<p>Here’s a sample listener implementation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.IDataProviderListener;
import org.testng.IDataProviderMethod;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
public static class SimpleDataProviderListener implements IDataProviderListener {
@Override
public void beforeDataProviderExecution(IDataProviderMethod dataProviderMethod, ITestNGMethod method, ITestContext iTestContext) {
log("Commencing", dataProviderMethod, method);
}
@Override
public void afterDataProviderExecution(IDataProviderMethod dataProviderMethod, ITestNGMethod method, ITestContext iTestContext) {
log("Completed", dataProviderMethod, method);
}
private static void log(String prefix, IDataProviderMethod dataProviderMethod, ITestNGMethod method) {
String msg = prefix + " execution of data provider : " + dataProviderMethod.getMethod().getName()
+ "() associated with the test method " + method.getQualifiedName() + "()";
System.err.println(msg);
}
@Override
public void onDataProviderFailure(ITestNGMethod method, ITestContext ctx, RuntimeException t) {
String msg = "The data provider " + method.getQualifiedName() + "() failed because of "
+ t.getMessage();
System.err.println(msg);
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>A sample test class that is using this listener.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.annotations.DataProvider;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners(SimpleDataProviderListener.class)
public class SampleDataDrivenTestCase {
@Test(dataProvider = "passing")
public void passingTest(int ignored) {}
@DataProvider(name = "passing")
public Object[][] getPassingDataProvider() {
return new Object[][] {
{1}, {2}
};
}
@Test(dataProvider = "failing")
public void skippedTest(int ignored) {}
@DataProvider(name = "failing")
public Object[][] getFailingDataProvider() {
throw new IllegalStateException("Initialisation failed");
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>The execution output would look like below:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="bash">SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Commencing execution of data provider : getPassingDataProvider() associated with the test method org.testng.demo.SampleDataDrivenTestCase.passingTest()
Completed execution of data provider : getPassingDataProvider() associated with the test method org.testng.demo.SampleDataDrivenTestCase.passingTest()
Commencing execution of data provider : getFailingDataProvider() associated with the test method org.testng.demo.SampleDataDrivenTestCase.skippedTest()
The data provider org.testng.demo.SampleDataDrivenTestCase.skippedTest() failed because of java.lang.IllegalStateException: Initialisation failed
... # Rest of the output omitted for brevity
===============================================
Default Suite
Total tests run: 3, Passes: 2, Failures: 1, Skips: 0
===============================================</code></pre>
</div>
</div>
</div>
<div class="sect2">
<h3 id="_listening_to_suite_level_invocations">Listening to Suite level invocations</h3>
<div class="paragraph">
<p>The listener {javadocs-base-url}/org/testng/ISuiteListener.html[ISuiteListener] allows you to be notified whenever TestNG is about to start processing a <code><suite></code>.</p>
</div>
<div class="paragraph">
<p>This listener can be used to perform setup/teardown activities at the suite level.</p>
</div>
<div class="paragraph">
<p>Add the listener implementation, as explained in the section about <a href="testng_listeners.html">TestNG listeners</a>.</p>
</div>
<div class="paragraph">
<p>Here’s a sample listener implementation.</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="java">import org.testng.ISuite;
import org.testng.ISuiteListener;
public class SimpleSuiteListener implements ISuiteListener {
@Override
public void onStart(ISuite suite) {
log("Commencing", suite);
}
@Override
public void onFinish(ISuite suite) {
log("Completed", suite);
}
private static void log(String prefix, ISuite suite) {
String msg = prefix + " execution for the suite named <" + suite.getName() + ">";
System.err.println(msg);
}
}</code></pre>
</div>
</div>
<div class="paragraph">
<p>A suite <code><suite></code> xml file could look like below:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="xml"><!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Regression-test-suite" verbose="2">
<listeners>
<listener class-name="org.testng.demo.SimpleSuiteListener"/>
</listeners>
<test name="P1-Build-Certification-Tests" verbose="2">
<classes>
<class name="org.testng.demo.SampleTestCase"/>
</classes>
</test>
</suite></code></pre>
</div>
</div>
<div class="paragraph">
<p>The execution output would look like below:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="prettyprint highlight"><code data-lang="bash">SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
...
... TestNG 7.9.0 by Cédric Beust ([email protected])
...
Commencing execution for the suite named <Regression-test-suite>
PASSED: org.testng.demo.configs.SampleTestCase.testMethod1
===============================================
P1-Build-Certification-Tests
Tests run: 1, Failures: 0, Skips: 0
===============================================
Completed execution for the suite named <Regression-test-suite>
===============================================
Regression-test-suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================
Process finished with exit code 0</code></pre>
</div>
</div>
</div>
</div>
<div id="footer">
<div id="footer-text">
Last updated 2025-02-27 18:51:48 UTC
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script>
</body>
</html>