我们正在使用 Geb 和 Spock 框架来测试 Web 应用程序并与范围报告集成。我们为范围报告功能创建了一个单独的类,使用 JUnit@Rule
在所有测试用例之间进行访问。现在的问题是当TC
失败时,它不会为失败的场景生成报告,而是为成功的案例创建报告。有什么方法可以TestWatcher
使用 Spock TC 捕获失败的异常。或者是否有任何类似TestWatcher
Spock 的类执行以下功能。
@Rule
public TestRule watchman = new TestWatcher() {
@Override
public Statement apply(Statement base, Description description) {
return super.apply(base, description)
}
@Override
protected void failed(Throwable e,Description description) {
TestUtils.takeScreenshot("testimg", "png", driver)
println("fail")
ExtentReports extent = createReport()
ExtentTest test = extent.startTest(description.getMethodName(), "Test failed, click here for further details")
// step log
String img = test.addScreenCapture(".testimg.png")
test.log(LogStatus.FAIL, "Failure : "+ e.toString(),img)
flushReports(extent, test)
}
@Override
protected void succeeded(Description description) {
ExtentReports extent = createReport()
ExtentTest test = extent.startTest(description.getMethodName(), "-")
test.log(LogStatus.PASS, "Passed")
flushReports(extent, test)
}
@Override
protected void skipped(AssumptionViolatedException e, Description description) {
ExtentReports extent = createReport();
ExtentTest test = extent.startTest(description.getMethodName(), "-")
// step log
test.log(LogStatus.SKIP, "Skipped")
flushReports(extent, test)
}
@Override
protected void finished(Description description) {
browser.close()
}
}
private ExtentReports createReport() {
ExtentReports extent = new ExtentReports(".testReport.html", false);
return extent
}
private void flushReports(ExtentReports extent, ExtentTest test){
extent.endTest(test)
extent.flush()
}