lundi 5 juin 2017

TestNG, Extent Report, retrying tests and removing retried tests

Hello: i am trying to use the extent reports to generate reports for the testNG tests. I have a solution as is provided below. I wanted to check with the community's expertise if there are better ways to do the same. Many thanks in advance.

Objective:

  • to retry failed tests onetime
  • to display results in extent report

Conditions:

  • extent report results must not include tests that were retried
  • not to use TestNG's IReporter

Questions:

  • Are there other efficient to do this?
  • Under Retry_Listener > onFinish where it is removing the skipped tests, is there a more meaningful way to do this?
  • Under Retry > verifyMaster where i am using extent to remove a retried test. Is there a better way to do this?

Result

verifyMaster:: Attempting test.
Retrying test verifyMaster with status FAILURE for the 1 time(s).
verifyMaster:: Attempting test.

===============================================
Suite
Total tests run: 1, Failures: 1, Skips: 0
===============================================

Retry.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://ift.tt/19x2mI9">
<suite name="Suite">
<listeners>
        <listener class-name="ExtentReport.Retry_Listener"/>
        <listener class-name="ExtentReport.Retry_Transformer"/>   
        <!--  <listener class-name="ExtentReport.Retry_Reporter"/>  -->                     
  </listeners>
  <test name="Test">
    <classes>
      <class name="ExtentReport.Retry"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Retry.java

public class Retry {

    ExtentReports extent;
    ExtentTest test;
    ExtentHtmlReporter htmlreporter;

    @BeforeClass
    void setup(){
        htmlreporter = new ExtentHtmlReporter(System.getProperty("user.dir") + "//test-output//Extent_retry.html");
        htmlreporter.setAppendExisting(false);

        extent = new ExtentReports();
        extent.attachReporter(htmlreporter);
        extent.setReportUsesManualConfiguration(true);
    }
    @AfterTest
    void teardown(){
        extent.flush();
    }   

    @Test(priority=1)
    public void verifyMaster(ITestContext context) {
        System.out.println("verifyMaster:: Attempting test.");
        test = extent.createTest("registration", "should validate if firstname is missing.");
        test.info("entering firstname");
        if(context.getAttribute("flag") != null){
            if(Boolean.parseBoolean(String.valueOf(context.getAttribute("flag"))) == true){
                extent.removeTest(test);
            }
        }
        test.fail("firstname was not visible");
        Assert.fail("firstname was not visible");
    }

}

Retry_Analyzer.java

public class Retry_Analyzer implements IRetryAnalyzer {
    private int retryCount = 0;
    private int maxRetryCount = 1;

    @Override
    public boolean retry(ITestResult result) {
        Boolean flag = false;       
        if (retryCount < maxRetryCount) {
            retryCount++;
            flag = true;
        }
        result.getTestContext().setAttribute("flag", flag);
        return flag;
    }
}

Retry_Transformer.java

public class Retry_Transformer implements IAnnotationTransformer{

    @Override
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
        IRetryAnalyzer retry = annotation.getRetryAnalyzer();
        if (retry == null)  {
            annotation.setRetryAnalyzer(Retry_Analyzer.class);
        }
    }
}

Retry_Listener.java

public class Retry_Listener implements ITestListener {
    @Override
    public void onFinish(ITestContext context) {
        Set<ITestResult> skippedTests = context.getSkippedTests().getAllResults();
        for (ITestResult temp : skippedTests) {
            ITestNGMethod method = temp.getMethod();
            if (context.getFailedTests().getResults(method).size() > 0) {
                skippedTests.remove(temp);
            } else {
                if (context.getPassedTests().getResults(method).size() > 0) {
                    skippedTests.remove(temp);
                }
            }
        }
    }
    .....
    .......
}

Aucun commentaire:

Enregistrer un commentaire