05
Apr

disable TestNG scripts execution at run-time

As part of Automation, we generally write scripts with the objective to run them at any given time.

Assume your framework need to detect certain failures before execution start and the need to execution needs to be stopped, we can utilize following code snippet:

Step 1: customize IAnnotationTransformer to disable all TestMethods :
——————————————————————–

import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class MyIAnnotationTransfer implements IAnnotationTransformer {

int count = 0;

public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
if (testMethod.getName() != null) {
annotation.setEnabled(false);
count++;
}
}

}

Step 2: associate above implementation to your TestNG scripts via testng.xml:
——————————————————————————







Step 3: your testNG script:
—————————

import org.testng.annotations.Test;

public class myTestNGScript {
@Test
public void myTest(){
System.out.println(“Hello world”);
}

@Test
public void myTest1(){
System.out.println(“Hello world”);
}

@Test
public void myTest2(){
System.out.println(“Hello world”);
}
}

Output:
——
[RemoteTestNG] detected TestNG version xxx.x.x

===============================================
Suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================