gpt4 book ai didi

grails - 如何在 Geb grails 中设置和拆卸功能测试数据

转载 作者:行者123 更新时间:2023-12-04 20:11:06 24 4
gpt4 key购买 nike

我有许多工作/通过的功能性 Geb/spock 测试(每个测试都扩展了 GebReportingSpec),它们正在使用测试数据测试 Web 应用程序,这些测试数据都是在功能测试套件开始时从 BootStrap.groovy 创建的。

我想将测试数据创建移动到每个 Spec 中的 startup()/teardown() 方法中,实际上我想让它们从基类继承它,但显然 StepWise 存在继承问题。

因此,目前我的每个测试规范类都类似于:

@Stepwise
class ExampleSpec extends GebReportingSpec {

def "valid root user logs in"() {

given: "I am at the login page"
to LoginPage

when: "I enter root's credentials"
username = "root"
password = "password"

and: "I click the login button"
loginButton.click()

then: "I am logged in and directed to the welcome page"
at WelcomePage
}
}

现在,我的问题是我似乎无法创建一个可以创建测试数据的新测试(在第一个测试之上)。如果没有有效的 given/when/then 语句,测试似乎不会被执行,并且从现有测​​试中调用方法似乎也不起作用。我已经研究了 grails-remote-control 插件来帮助我,我相信这将使我能够成功地调用闭包来设置数据,但我不确定从 GebReportSpecs(或某些抽象父)中调用它的最佳机制.

下面是我想要做的事情的简要概述,无论是通过将“setupData()”作为第一个测试还是通过从测试中调用该方法......似乎都不起作用。
def remote = new RemoteControl()
def setupData() {

def id = remote {
def ShiroUser user = new ShiroUser(username: "root", ...)
user.save()
user.id
}
println(id)
}

.... Tests then follow

是否有任何像@before 之类的注释可以强制调用这些方法?

任何建议表示赞赏。

解决方案:
我已经接受了下面 dmahapatro 在正确答案中的回复,但也为那些可能觉得有用的人提供了我的最终解决方案示例。

最佳答案

(未经测试)
GebReportingSpec扩展 GebSpec最终扩展 spock.lang.Specification其中有 Fixture Methods .

您可以像这样使用它们:

@Stepwise
class ExampleSpec extends GebReportingSpec {
def setupSpec(){
super.setupSpec()
//setup your data
}

def cleanupSpec(){
super.cleanupSpec()
//I do not think you would need anything else here
}

def "This is test 1"(){

}

def "This is test 2"(){

}
}

您不能使用 setup 作为您的测试方法之一,因为不会为单个测试用例维护状态。它是这样的:-
setup called -> test1 -> teardown called  
setup called -> test2 -> teardown called
setup called -> test3 -> teardown called
.........

关于grails - 如何在 Geb grails 中设置和拆卸功能测试数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17149239/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com