2019년 7월 8일 월요일

TestNG에 ExtentReport 적용하기

1. pom.xml dependency 추가

<dependency>    
  <groupId>com.aventstack</groupId>    
  <artifactId>extentreports</artifactId>    
  <version>3.1.5</version>
</dependency>

2. ExtentManager 추가

public class ExtentManager {
    private static ExtentReports extent;

    public static ExtentReports getInstance() {
        if (extent == null)
            createInstance();
        return extent;
    }

    //Create an extent report instance
    public static ExtentReports createInstance() {
        String reportFile = new File("target/result.html").getAbsolutePath();
        ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(reportFile);
        htmlReporter.setAppendExisting(true);
        htmlReporter.config().setTestViewChartLocation(ChartLocation.BOTTOM);
        htmlReporter.config().setChartVisibilityOnOpen(true);
        htmlReporter.config().setTheme(Theme.STANDARD);
        htmlReporter.config().setDocumentTitle(reportFile);
        htmlReporter.config().setEncoding("UTF-8");
        htmlReporter.config().setReportName(reportFile);

        extent = new ExtentReports();
        extent.attachReporter(htmlReporter);
        extent.setSystemInfo("Environment", "QA");
        TestData testData = new TestData();
        extent.setSystemInfo("Host Name", testData.getTestData().getUri());
        return extent;
    }
}

3. TestListener 추가

public class TestListner implements ITestListener {
    //Extent Report Declarations
    private static final Logger logger = LoggerFactory.getLogger(TestListner.class);
    private static ExtentReports extent = ExtentManager.createInstance();
    private static ThreadLocal<ExtentTest> test = new ThreadLocal<>();

    @Override
    public synchronized void onStart(ITestContext context) {
        logger.debug("Test Suite started!");
    }

    @Override
    public synchronized void onFinish(ITestContext context) {
        logger.debug("Test Suite is finished!");
        extent.flush();
    }

    @Override
    public synchronized void onTestStart(ITestResult result) {
        logger.debug(result.getMethod().getMethodName() + " started!");
        ExtentTest extentTest = extent.createTest(result.getMethod().getMethodName(),result.getMethod().getDescription());
        extentTest.assignCategory(result.getTestClass().getRealClass().getSimpleName());
        test.set(extentTest);
    }

    @Override
    public synchronized void onTestSuccess(ITestResult result) {
        logger.debug(result.getMethod().getMethodName() + " passed!");
        test.get().pass("Test passed");
    }

    @Override
    public synchronized void onTestFailure(ITestResult result) {
        logger.debug(result.getMethod().getMethodName() + " failed!");
        test.get().fail(result.getThrowable());
    }

    @Override
    public synchronized void onTestSkipped(ITestResult result) {
        logger.debug(result.getMethod().getMethodName() + " skipped!");
        test.get().skip(result.getThrowable());
    }

    @Override
    public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
        logger.debug("onTestFailedButWithinSuccessPercentage for " + result.getMethod().getMethodName());
    }
}

4. testng.xml에 추가
<suite name="TestNG Suite Name">
    <listeners>
        <listener class-name="com.xxxx.utils.listners.TestListner" />    
    </listeners>
    <test name="full_test">
         <packages>
             <package name="com.xxx.test.*" />
         </packages>
     </test>
</suite>

댓글 없음:

댓글 쓰기