I have a situation where I need to enable/disable tests(using AnnotationTransformer listener) based on the parameter coming from my suite files(using ISuiteListener listener). However, when I try to read the parameters in my suite file before calling AnnotationTransformer, I am not able to do so.
This does not help:
<listeners preserve-order="true">
<listener class name="automation.test.SentinelSuiteListener" />
<listener class-name="automation.test.AnnotationTransformer" />
</listeners>
Also, when I try to implement both these interfaces together, the method for AnnoataionTransformer i.e. transform goes before the onStart() method:
public class AnnotationTransformer implements IAnnotationTransformer, ISuiteListener {
String currentRunModeInSuite;
@Override
public void onStart(ISuite suite) {
currentRunModeInSuite = suite.getParameter("currentRunMode");
}
@Override
public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
System.out.println("***Test case under review : " + testMethod.getName());
for(int i=0; i<annotation.getGroups().length;i++){
System.out.println(annotation.getGroups()[i]);
}
System.out.println(currentRunModeInSuite);
}
@Override
public void onFinish(ISuite suite) {
// TODO Auto-generated method stub
}
}
The behavior you seen is the expected one because TestNG works step by step.
First, it looks for tests and reads their datas. You can be triggered if you want to change them and it's the goal of IAnnotationTransformer
.
Then, TestNG runs suite/tests/classes/methods and you can be triggered by that with ISuiteListener
or else.
What you need is IMethodInstance
which is called during the run: http://testng.org/doc/documentation-main.html#methodinterceptors
Fyi, the <listeners>
node doesn't accept preserve-order
attribute. See the documentation: http://testng.org/testng-1.0.dtd.php