Monday 17 September 2018

TestNg | Rerun a failed test case ; Amazon interview qn

Option# 1. testng-failed.xml file in test-output directly
when we run any test suite tesng generates testng-failed.xml file if any failure happens. Just go there and right click on it and Run it.

Option#2 IRetryAnalyzer

public class Retry implements IRetryAnalyzer{
int count =0, retryCount = 3;
   public boolean retry(ITestResult result){
       if(count<retryCount){
           count++;
           return true;
         }
    }
}

In Test file
@Test(retryAnalizer=Retry.class)
public void m1(){
}


Option#3: IAnnotationTransformer
a. Use the same Retry class here

b. public class Transformation implement IAnnotationTrnasformer
{
          @Override
          public void transform(ITestAnnotation annotation, Class testClass, Constructor                    testConstructor,Method test method){
           IRetryAnalyzer  retry = annotation.getRetryAnalyzer();
            if(retry == null){
            annoatation.setRetryAnalyzer(Retry.class);
                 }
           }
}
c. add a listener
<listeners>
<listener class-name="com.google.Transformation">
</listners>

TestNG : Hard vs Soft assertion

Soft : Test Fails for sure, but execution Do not Stops even any assertion condition fails.
SoftAssert s = new SoftAssert();
s.assertEquls(1,1);
s.assertEquls(2,1);
s.assertAll();

Hard: Test Fails & STOPS then and their where any assertion condition fails.
Assertion a = new Assertion();
a.assertEquls(1,1);

Testng - how to skip a test case at run time

@Test
public void m1(){
if(aboveMethodFailed)
throw new SkipException("Skipped");
}

using
throw new SkipException("Skipped"); statement we can skip an test case.