2

We are using Geb and Spock framework for testing web application and integrated with an extent report. We have created a separate class for extent report functionalities using JUnit @Rule to access among all Test cases. Now the problem is when TC fails, it's not generating a report for a failed scenario, instead it's creating a report for succeeded case. Is there any way to catch TestWatcher failed exception using Spock TC's. Or is there any similar class like TestWatcher in Spock which performs below functionality.

@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()
}
Craig
  • 7,471
  • 4
  • 29
  • 46
Vivetha
  • 41
  • 4

1 Answers1

0

Spock has some basic support for JUnit rules.

If your particular case does not work you need to write a Spock extension that gives you hookups to the test lifecycle.

See also Execute some action when Spock test fails

Community
  • 1
  • 1
kazanaki
  • 7,988
  • 8
  • 52
  • 79