selenium-webdriver - 如果设置方法中发生错误,则 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'
解决方案
以下是我使用的解决方法。
在基类中:
@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 可以看到并记录测试。
推荐阅读
- r - 具有多个观测值、变量和组的马氏距离
- python - 除2概率极弱
- java - Eclipse 2020-09:尝试使用插件运行 WebSphere AS 8.5 服务器时出现 org/omg/CORBA/UserException
- xml - 为什么 xsl:value 没有选择文本?
- java - 如何从 Java 中的大型远程 zip 文件下载单个文件?
- acumatica - 将属性从库存项目拉到 SO 行自定义字段
- android - android:useEmbeddedDex 尝试合并到项目清单文件时未找到
- python - How can I run a for loop and append items to a list at the end of the loop?
- symfony - 如何在 XML 文件格式学说 ORM 中添加级联参数
- python - 线程函数完成后调用主线程中的函数