- 美化后效果
美化后的报告,页面清晰简洁。重要信息都可以体现出来,用例通过率,失败的用例和失败原因
- 主要技术点
-
ExtentReports
-
JUnit的@Rule
-
重写TestWatcher的succeeded和failed方法
- 代码
ExtentReports maven引入
<dependency> <groupId>com.relevantcodes</groupId> <artifactId>extentreports</artifactId> <version>2.41.2</version> </dependency>
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/