How to modify the TestNG annotation’s value at run-time
Following code will never get executed
import org.testng.annotations.Test;
public class ExampleTestNG {
@Test(enabled = false)
public void invoke() {
System.out.println(“Function invoked…..”);
}
}
Output:
——-
===============================================
Default suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================
[TestNG] No tests found. Nothing was run
In order to dynamically change attribute “enable = true”, TestNG provides customization of “IAnnotationTransformer”. We can use following steps to override current attributes values at run time, by following below steps:
Step 1: Create a custom implementation of “IAnnotationTransformer” interface which checks if “enabled=false” convert it to “true”
———————————————————————————————————————————–
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;
public class Transformer implements IAnnotationTransformer {
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
if (!isTestConfigured(annotation)) {
System.out.println(“Start Transformation”);
annotation.setEnabled(true);
}
}
public boolean isTestConfigured(ITestAnnotation a) {
boolean res = false;
if (a.getEnabled()==true) {
res = true;
}
return res;
}
}
Step 2: Configure testng.xml to associate listeners :
—————————————————–
Configure “
Step 3: execute TestNG functions via above testng.xml:
——————————————————
Output:
Start Transformation
Function invoked…..
===============================================
Suite
Total tests run: 1, Failures: 0, Skips: 0
===============================================
Few of the TestNG Attributes are:
timeOut
alwaysRun
dataProvider
dataProviderClass
dependsOnGroups
description
enabled
expectedExceptions
expectedExceptionMessageRegExp
groups
ignoreMissingDependencies
invocationCount
invocationTimeOut
parameters
priority
retryAnalyzer
sequential (Depreciated – use singleThreaded instead)
singleThreaded
skipFailedInvocations
successPercentage
suiteName
testName
threadPoolSize
0 comments