The requirement in the comment is not possible using Annotation transformer. This is because the transformer is run before any test is run. This is done to finalize the annotations before test execution starts.
The field m_enabled
is used to determine if the test method could be run or not. Even though you could change the m_enabled
to false
midway during test execution, it does not help as the test execution has reached a point where the tests that are to be run are already finalized and m_enabled
is not checked afterwards.
So for this requirement, which disables a test method using the result of a different test such that the disabled test would not appear on the test report is not possible. But, based on your comment "What if I generate a random number between 1 or 2. If 1, then execute test path A. If 2, then execute test path B", in this case you could use the transformer to enable/disable certain methods because this logic is independent of any test result. For this you could define a transformer as follows:
public class MyTransformer implements IAnnotationTransformer {
private final List<String> pathOneMethods = List.of("testA","testB");
private final List<String> pathTwoMethods = List.of("testB","testC");
private final Random r = new Random(); //java.util
private final List<String> selectedPath;
public MyTransformer() {
selectedPath = (r.nextBoolean()) ? pathOneMethods : pathTwoMethods;
}
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
if(testMethod != null && !selectedPath.contains(testMethod.getName())) {
annotation.setEnabled(false);
}
}
}
Annotation transformers cannot be included as part of @Listeners
in the code. It has to be specified in the testng.xml
as <listeners>
inside the <suite>
tag.
<suite>
................
................
<listeners>
<listener class-name = "com.code.MyTransformer"></listener>
</listeners>
................
................
</suite>