1.2. Example of IAnnotationTransformer

1.2. Example of IAnnotationTransformer



IAnnotationTransformer is a TestNG allows you to do using its annotation transformation framework. In general annotations are static in nature by design, so any change in the values require recompilation of source files. Since TestNG relies heavily on annotations, it would be nice if one can override its behavior at runtime. 
There are four different ways to achieve this

1.2.1. Example of @Test annotation transformer

1.2.2. Example of @DataProvider annotation transformer

1.2.3. Example of @Factory annotation transformer

1.2.4. Example of Configuration annotation transformer


===========================================================================

1.2.1. Example of @Test annotation transformer

Representation :

 

 In the below example, we configure the @Test annotation.
TestAnnotationTransformerExample is our test class. It contains test methods testcase1, testcase2 and testcase3. Methods testcase1 and testcase2 accept a string parameter but we haven’t provided any DataProvider. The DataProvider will be set on-the-fly in the annotation transformer, based on the method, and also by using Annotation.setEnabled(fasle) will disable the test case. 

TestAnnotationTransformerExample;

 



1.2.2. Example of @DataProvider annotation transformer

If we want to modify any TestNG annotation besides @Test then we can make use of  @Dataprovider annotation transformer. In this example, based on the dataProvider, we decide whether it should be used in parallel. If the dataProvider returns a "getDP3", we run it in parallel.

   

1.2.3. Example of @Factory annotation transformer

In this example, we transform a factory annotation.
FactoryAnnotationTransformerExample is a test class which depends on @Factory annotation for its creation. We will modify the annotation dynamically to set its source to a DataProvider.

  

1.2.4. Example of Configuration annotation transformer

In this example, we will alter the configuration based annotations like @BeforeSuite, @BeforeTest etc.
ConfigurationAnnotationTransformerExample is the test class. It contains some configuration methods with a descriptionattribute.
  

 

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.