Conditional testing with ITestAnnotation interface

In TestNG there is provision that we can run tests conditionally, like we can have test case that needed to be executed with flags yes or no and we want to conditionally run these tests.

For this purpose we can make use of ITestAnnotation interface. This provides the interface to override the test annotations during runtime.

TestNG_ItestAnnotation

The above example is listener class that can override the transform method, which dictates the test annotation. We can control the enabled annotation attribute to make test available to pickup by the TestNG framework.

Make sure that you have enabled annotation available for the test. I.E explicitly add this annotation for the test so that, TestNG overrides the Annotation. If this attribute is not there TestNG cannot  override the same.

TestNG_ITestAnnotation_Enabled

And finally add this listener to TestNG XML to make things work.

TestNG_XML