标签:
最近使用了extentreports,代替了原来简陋的测试报告。下面来分享一下成果和心得。
@Rule public ReportMaker reportRule = new ReportMaker(driver);
将具体的逻辑写到ReportMaker里面,写法是继承TestWatcher,这个是junit rule里提供的类,用来获取运行状态和结果,可以重写failed和succeeded实现错误信息截图和生成报告。
以failed方法为例:
@Override protected void failed(Throwable e, Description description) { // 截图 TakesScreenshot takesScreenshot = (TakesScreenshot) browser; File scrFile = takesScreenshot.getScreenshotAs(OutputType.FILE); File destFile = getDestinationFile(description); try { FileUtils.copyFile(scrFile, destFile); } catch (IOException ioe) { throw new RuntimeException(ioe); } ExtentReports extent = createReport(); ExtentTest test = extent.startTest(description.getDisplayName(), "Test failed, here for further details"); // 记录错误信息 test.log(LogStatus.FAIL, e); // 由于我有出错重跑机制,故进行分类 if (methodName.contains(description.getDisplayName())){ test.assignCategory("Retry Class Result"); }else { test.assignCategory("First Run Class Result"); } methodName.add(description.getDisplayName()); flushReports(extent, test); }
succeeded方法只要不记录错误信息即可
创建和结束report的方法参考官网上的
private ExtentReports createReport() { ExtentReports extent = new ExtentReports(filenameOfReport, false); return extent; } private void flushReports(ExtentReports extent, ExtentTest test){ // ending test extent.endTest(test); // writing everything to document extent.flush(); }
结果展示:
标签:
原文地址:http://www.cnblogs.com/clarkar/p/5168848.html