Search code examples
javaunit-testingselenium-webdrivertestngtestng-eclipse

Setting test method priority in TestNG XML


Question: How do I set method priority in TestNG XML rather than adding it at class.method level with priority tag?

I tried finding out answer for this but I was not able to find this specific point anywhere.

Use Case: I can generating TestNG XML Programatically and invoking test methods based on the given method names written in an external file as a datasource.


Solution

  • I think there is more than one way to do this. Using a listener that implements Annotation Transformer is one of them. You can do something like this:

    public class SetPriorityListener implements IAnnotationTransformer {
    
        @Override
        public void transform(final ITestAnnotation annotation, final Class testClass,
            final Constructor constructor, final Method method) {
            if ("myTestName".equals(method.getName())) {
                annotation.setPriority(getTestPriority());
            }
        }
    
        private int getTestPriority() {
            //logic to get priority for this test
            return 0;
        }
    }
    

    You can read more about AnnotationTransformer in the official documentation: http://testng.org/doc/documentation-main.html#annotationtransformers