首页 > 解决方案 > 如果设置方法中发生错误,则 Allure 报告中缺少规范

问题描述

在我的 GEB + Selenium Webdriver 测试中,方法中有一些 UI 操作setupSpec()(我相信常见情况)。问题是,如果在这些步骤中出现异常,则整个规范在最终报告中完全丢失,但是在报告中看到该规范的测试被标记为被忽略是合乎逻辑的。

这是一个真正的问题,因为构建可以通过 100% 的成功率,尽管有没有开始的测试。

\build\allure-results\<id>-result.json不会为该规范生成文件。这是重现该问题的示例:

Spec #1(有一个例外,预计将显示为忽略):

@Stepwise
@Feature("Job")
@Story("Spec with exception in setup")
class SetupExceptionTest extends GebReportingSpec {

    def setupSpec() {
        println 'in setup spec'
        throw new ElementNotInteractableException('some error')
    }

    def 'Test 1'() {
        setup:
        println 'in test 1'
        expect:
        2 == 2
    }

    def 'Test 2'() {
        setup:
        println 'in test 2'
        expect:
        2 == 3
    }

    def cleanupSpec() {}
}

Spec #2(设置中没有例外):

@Stepwise
@Feature("Job")
@Story("Spec with no exception in setup")
class SetupTest extends GebReportingSpec {

    def setupSpec() {
        println 'in setup spec'
    }

    def 'Test 1'() {
        setup:
        println 'in test 1'
        expect:
        2 == 2
    }

    def 'Test 2'() {
        setup:
        println 'in test 2'
        expect:
        2 == 3
    }

    def cleanupSpec() {}
}

启动命令:gradlew clean test -PignoreTestFailures=true allureServe生成以下报告:

在此处输入图像描述

因此,任何报告部分都没有SetupExceptionTest规范。是否有任何设置可以更改此行为?或者可能是已知的解决方法?

使用了以下版本:

testCompile group: 'org.spockframework', name: 'spock-core', version: '1.2-groovy-2.4'
testCompile group: 'io.qameta.allure', name: 'allure-spock', version: '2.7.0'

标签: selenium-webdriverspockgeballureautotest

解决方案


以下是我使用的解决方法。

在基类中:

@Shared
protected Throwable setupSpecThrowable

def setup() {
    if (setupSpecThrowable) {
        throw setupSpecThrowable
    }
}

def cleanupSpec() {
    setupSpecThrowable = null
}

protected def withSetupErrorCatch(Closure closure) {
    try {
        closure()
    } catch (Throwable t) {
        setupSpecThrowable = t
    }
}

在每个测试类中:

def setupSpec() {
    def data = getSetupSpecData()
   
    withSetupErrorCatch {

        //actions that can throw something
        
    }
}

这会捕获任何异常setupSpec()并将其抛出,setup()以便 allure 可以看到并记录测试。


推荐阅读