You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/asciidoc/docs/annotation_transformers.adoc
+40-6Lines changed: 40 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -1,29 +1,62 @@
1
1
==== Annotation Transformers
2
2
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.
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.
6
6
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.
8
8
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
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.)
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
+
27
60
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.
28
61
29
62
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 {
0 commit comments