5

This works as supposed, test fails (due to haltTesting()) and is repeated 2x

public class A0001_A0003Test extends TestControl {

    private Kunde kunde = Kunde.FR_WEHLITZ;

    @Test(retryAnalyzer = TestRepeat.class, groups = {TestGroups.FAILED}, description = "verify adress")
    public void testkundenDaten_Angaben() throws Exception {
        bifiTestInitial();
        testActions.selectKunde(kunde);
        haltTesting();
    }
} 

but because i have multiple tests in one class, i defined the repeatAnalyzer on class level

@Test(retryAnalyzer = TestRepeat.class)
public class A0001_A0003Test extends TestControl {

    private Kunde kunde = Kunde.FR_WEHLITZ;

    @Test(groups = {TestGroups.FAILED}, description = "verify adress")
    public void testkundenDaten_Angaben() throws Exception {
        bifiTestInitial();
        testActions.selectKunde(kunde);
        haltTesting();
    }
} 

but then the test is not repeated, the documentation says:

The effect of a class level @Test annotation is to make all the public methods of this class to become test methods even if they are not annotated. You can still repeat the @Test annotation on a method if you want to add certain attributes.

So it should have been possible or am I expecting the wrong outcome?

DT7
  • 1,615
  • 14
  • 26
MushyPeas
  • 2,469
  • 2
  • 31
  • 48
  • "The effect of a class level @Test annotation is to make all the public methods of this class to become test methods even if they are not annotated." It should have been mentioned in the javadoc of `@Test` and `retryAnalyzer()`. – Anderson Mar 16 '22 at 06:04

2 Answers2

2

My solution was to set a retryAnalyzer for all methods in the @BeforeSuite method. But do not set it in beforeMethod because then it will be re-created each invocation with a new counter => endless loop.

@BeforeSuite(alwaysRun = true)
public void beforeSuite(ITestContext context) {
     TestRepeat testRepeat = new TestRepeat();
     for (ITestNGMethod method : context.getAllTestMethods()) {
         method.setRetryAnalyzer(testRepeat);
     }
}
Community
  • 1
  • 1
MushyPeas
  • 2,469
  • 2
  • 31
  • 48
  • Is there a reason you instantiate a new TestRepeat class for each method or is it kosher to use a single object for all methods in its place? (aka, singleton or just new TestRepeat() object created before the for loop)? – RockMeetHardplace Mar 12 '14 at 14:33
  • `TestRepeat implements IRetryAnalyzer` but using `method.setRetryAnalyzer(TestRepeat.class);` gives me an error in NetBeans (not a Java Pro :/) `incompatible Types Class cannot be converted to IRetryAnalyzer` – MushyPeas Mar 12 '14 at 19:10
1

You can implement IAnnotationTransformer listener and register listener cmd line or in config file or at class level.

public class MyAnnotationTransformer implements
        IAnnotationTransformer {
    @Override
public void transform(ITestAnnotation testAnnotation, Class clazz, Constructor testConstructor,
        Method method) {
            testAnnotation.setRetryAnalyzer(TestRepeat.class);
}
...
}

To register at class level:

@Listeners(value=MyAnnotationTransformer.class)
public class A0001_A0003Test extends TestControl {
...
}
user861594
  • 5,733
  • 3
  • 29
  • 45
  • I believe this blog post deserves more exposure along with above answer. https://www.swtestacademy.com/retry-failed-tests-testng-iretryanalyzer/ – Changgull Song May 09 '19 at 23:58