JUnit报告美化——ExtentReports

  • 美化后效果
美化后的报告,页面清晰简洁。重要信息都可以体现出来,用例通过率,失败的用例和失败原因

JUnit报告美化——ExtentReports_第1张图片

  • 主要技术点
  1. ExtentReports
  2. JUnit的@Rule
  3. 重写TestWatcher的succeeded和failed方法
  • 代码
ExtentReports maven引入
        
            com.relevantcodes
            extentreports
            2.41.2
        

ExtentUtils类,继承TestWatcher,并重写succeeded和failed

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

/**
 * Created by wangx on 17/9/25.
 * #
 */
public class ExtentUtils extends TestWatcher {

    private ExtentReports extent;

    public ExtentUtils(ExtentReports extent) {
        this.extent = extent;
    }

    @Override
    protected void succeeded(Description description) {
        ExtentTest test = extent.startTest(description.getDisplayName(), "-");

        // step log
        test.log(LogStatus.PASS, "-");
        flushReports(extent, test);
    }

    @Override
    protected void failed(Throwable e, Description description) {
        ExtentTest test = extent.startTest(description.getDisplayName(), "Test failed");

        // step log
        test.log(LogStatus.FAIL, e);
        flushReports(extent, test);
    }

    private void flushReports(ExtentReports extent, ExtentTest test){
        // ending test
        extent.endTest(test);
        // writing everything to document
        extent.flush();
    }
}

测试用例

import com.jhj.http.commom.ExtentUtils;
import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.NetworkMode;
import org.junit.*;

/**
 * Created by wangx on 17/9/26.
 *
 */
public class Demo {

    private static ExtentReports extent;

    @BeforeClass
    public static void beforeClass() {
        String reportPath = "reports/demo/extent.html";
        extent = new ExtentReports(reportPath, true, NetworkMode.OFFLINE);
    }

    @AfterClass
    public static void afterClass() {
        extent.close();
    }

    @Rule
    public ExtentUtils eu = new ExtentUtils(extent);

    @Test
    public void willPass() {
        Assert.assertEquals(1, 1);
    }

    @Test
    public void willFail() {
        Assert.assertEquals(1, 2);
    }
}

运行后生成html文件,<工程跟目录>/reports/demo/extent.html

参考资料:http://extentreports.com/docs/versions/2/java/

 

转载于:https://www.cnblogs.com/goldsking/p/7598085.html

你可能感兴趣的:(JUnit报告美化——ExtentReports)