Joh*_*ton 5 grails test-data functional-programming spock geb
我有很多工作/传递函数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
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是我似乎无法创建可以创建测试数据的新测试(在第一个测试之上).没有有效的给定/ 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
Run Code Online (Sandbox Code Playgroud)
是否有像@before等注释可以强制这些方法被调用?
任何建议表示赞赏.
解决方案:我已经在正确的答案中接受了下面的dmahapatro的回复,但我也提供了一个我最终解决方案的例子,供那些可能觉得有用的人使用.
(未经测试)
GebReportingSpec扩展了GebSpec,后者最终扩展spock.lang.Specification了FixtureMethods。
您可以像这样使用它们:
@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"(){
}
}
Run Code Online (Sandbox Code Playgroud)
您不能使用设置作为测试方法之一,因为不会为单个测试用例维护状态。事情是这样的:-
setup called -> test1 -> teardown called
setup called -> test2 -> teardown called
setup called -> test3 -> teardown called
.........
Run Code Online (Sandbox Code Playgroud)