Skip to content

Commit e39c1dc

Browse files
committed
Enhancing docs for IAnnotationTransformer
Closes #68
1 parent 5af237a commit e39c1dc

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

src/main/asciidoc/docs/annotation_transformers.adoc

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,62 @@
11
==== Annotation Transformers
22

3-
TestNG allows you to modify the content of all the annotations at runtime. This is especially useful if the annotations in the source code are right most of the time, but there are a few situations where you'd like to override their value.
3+
:url: https://javadoc.io/static/org.testng/testng/{version-label}
44

5-
In order to achieve this, you need to use an Annotation Transformer.
5+
TestNG allows you to modify the content of all the annotations at runtime.
66

7-
An Annotation Transformer is a class that implements {javadocs-base-url}/org/testng/IAnnotationTransformer.html[IAnnotationTransformer]
7+
This is especially useful if the annotations in the source code are right most of the time, but there are a few situations where you'd like to override their value.
88

9-
Like all the other TestNG listeners, you can specify this class either on the command line or with ant:
9+
In order to achieve this, you can build a class that implements {url}/org/testng/IAnnotationTransformer.html[IAnnotationTransformer]
10+
11+
This is a special TestNG listener. It can be added into TestNG via the following mechanisms.
12+
13+
==== Via xml suite file
14+
15+
You can use the `<listeners>` tag to specify an implementation of `IAnnotationTransformer` in your suite xml file.
16+
17+
==== Via command line arguments
18+
19+
You can use the command line argument `-listener` to specify the fully qualified class name of the implementation of
20+
`IAnnotationTransformer` as shown below.
1021

1122
[source, bash]
1223

1324
----
1425
java org.testng.TestNG -listener MyTransformer testng.xml
1526
----
1627

17-
or programmatically:
28+
==== Via your code
29+
30+
An implementation of `IAnnotationTransformer` can be wired in via your code as well (In case you are working with using the TestNG APIs for programmatically running your tests.)
1831

1932
[source, java]
2033

2134
----
2235
TestNG tng = new TestNG();
23-
tng.setAnnotationTransformer(new MyTransformer());
36+
tng.addListener(new MyTransformer());
2437
// ...
2538
----
2639

40+
WARNING: Please don't use the `@Listeners` annotation to wire-in an implementation of `org.testng.IAnnotationTransformer`.
41+
Doing so will cause your implementation to be ignored. This is because TestNG needs to be able to parse all annotations
42+
before starting to execute them and `@Listeners` is also one such annotation.
43+
44+
The annotation transformer allows you to alter the below types of annotations at runtime:
45+
46+
* `@Test` annotation on test methods.
47+
* Any of the common attributes associated with the below listed configuration annotations:
48+
** `@BeforeSuite`
49+
** `@AfterSuite`
50+
** `@BeforeTest`
51+
** `@AfterTest`
52+
** `@BeforeClass`
53+
** `@AfterClass`
54+
** `@BeforeMethod`
55+
** `@AfterMethod`
56+
* `@Listeners` annotation on test classes.
57+
* `@Factory` annotation used to mark constructors or a factory method as test factories.
58+
* `@DataProvider` annotated data providers.
59+
2760
When the method `transform()` is invoked, you can call any of the setters on the `ITestAnnotation` test parameter to alter its value before TestNG proceeds further.
2861

2962
For example, here is how you would override the attribute invocationCount but only on the test method invoke() of one of your test classes:
@@ -39,3 +72,4 @@ public class MyTransformer implements IAnnotationTransformer {
3972
}
4073
}
4174
----
75+

0 commit comments

Comments
 (0)