使用extentreports美化报告

无意之间在整理testng 报告输出的文档时,发现一个美化testng的报告的插件,感觉确实“漂亮”,但是还不确定是否实用,案例来自官方网站自己添了一些内容,更改了存放路径,本地目前已确定可正常运行,官方网址:http://extentreports.com/documentation/version-2/

1.配置maven依赖

<dependency>
			    <groupId>com.relevantcodes</groupId>
			    <artifactId>extentreports</artifactId>
			    <version>2.40.2</version>
			</dependency>

2.创建一个测试类,我是mytest

自己添加了5个测试方法,3个正确,2个错误

image

查看展示效果

11

image

image

demo代码

package com.test.appuimtest;

import org.apache.tools.ant.util.facade.FacadeTaskHelper;
import org.testng.Assert;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

import until.publicmethod;

public class mytest {
    public class SingleLogTest extends BaseExample {
        @Test
        public void passTest() {
            test = extent.startTest("passTest");
            test.log(LogStatus.PASS, "Pass");
            Assert.assertEquals(test.getRunStatus(), LogStatus.PASS);
        }
        
        @Test
        public void intentionalFailure() throws Exception {
            test = extent.startTest("intentionalFailure");
            throw new Exception("intentional failure");        
        }
        
        @Test
         public void Mytest1(){
              test = extent.startTest("Mytest1");
              Assert.assertTrue(true);
         }
        @Test
         public void Mytest2(){
              test = extent.startTest("Mytest2");
              Assert.assertTrue(true);
         }
        @Test
         public void Mytest3(){
              test = extent.startTest("Mytest2");
              Assert.assertTrue(false);
         }
    }

    public static class ExtentManager {
        private static ExtentReports extent;
        
        public synchronized static ExtentReports getReporter(String filePath) {
            if (extent == null) {
                extent = new ExtentReports(filePath, true);
                
                extent
                    .addSystemInfo("Host Name", "Anshoo")
                    .addSystemInfo("Environment", "QA");
            }
            
            return extent;
        }
    }

    public abstract class BaseExample {
        protected ExtentReports extent;
        protected ExtentTest test;  
        final String filePath = "d:\Extent.html"; //这里需要更改创建的存放路径

        @AfterMethod
        protected void afterMethod(ITestResult result) {
            if (result.getStatus() == ITestResult.FAILURE) {
                test.log(LogStatus.FAIL, result.getThrowable());
            } else if (result.getStatus() == ITestResult.SKIP) {
                test.log(LogStatus.SKIP, "Test skipped " + result.getThrowable());
            } else {
                test.log(LogStatus.PASS, "Test passed");
            }
            
            extent.endTest(test);        
            extent.flush();
        }
        
        @BeforeSuite
        public void beforeSuite() {
            extent = ExtentManager.getReporter(filePath);
        }
        
        @AfterSuite
        protected void afterSuite() {
            extent.close();
        }
    }
}

  

 在继续补充,发现在报告下面还可以增加图片和视频,添加出来的效果如下

原文地址:https://www.cnblogs.com/chongyou/p/7518959.html