如何设置和拆卸Geb grails中的功能测试数据 [英] How to setup and teardown functional test data in Geb grails

查看:159
本文介绍了如何设置和拆卸Geb grails中的功能测试数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多工作/传递函数geb / spock测试(每个扩展了GebReportingSpec),这些测试正在测试一个web应用程序,测试数据都是在功能测试套件开始时从BootStrap.groovy创建的。



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



所以,目前我的每个测试规范类看起来都像这样:

  @Stepwise 
class ExampleSpec扩展GebReportingSpec {

def有效的root用户登录(){

给出:我在登录页面
到LoginPage

当:我输入root的凭证
username =root
password =password

和:我点击登录按钮
loginButton.click()

然后:我登录了我n,然后转到欢迎页面Welcome b


code


现在,我的问题是,我似乎无法创建一个可以创建测试数据的新测试(在第一个测试之上)。如果没有有效的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)
}

....然后测试

有没有像@before等注解可以强制这些方法被调用?





解决方案:
我接受ed dmahapatro在下面的回答中给出了正确的答案,但也提供了一个我最终解决方案的例子,以供那些可能会觉得有用的人使用。

未经测试

GebReportingSpec 扩展 GebSpec ,它最终扩展了 spock.lang.Specification ,它具有夹具方法



您可以像这样使用它们:

  @Stepwise 
class ExampleSpec扩展GebReportingSpec {
def setupSpec(){
super.setupSpec()
//设置你的数据
}

def cleanupSpec(){
super.cleanupSpec()
//一世不要以为你会在这里需要任何东西

$ b $ def这是测试1(){

}

def This is test 2(){

}
}

您不能将setup设置为您的测试方法之一,因为不会为单个测试用例维护状态。它是这样的: -

  setup called  - > test1  - >拆解称为
setup称为 - > test2 - >拆解称为
setup称为 - > test3 - >拆解叫做
.........


I have many working/passing functional geb/spock tests (each extending GebReportingSpec) that are testing a web application with test data all created from the BootStrap.groovy at the beginning of the functional test suite.

I want to move the test data creation into startup() / teardown() methods within each Spec, well actually I wanted to have them inherit it from a base class but apparently the StepWise has issues with inheritance.

So, at present each of my test spec classes look something like:

@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
    }
}

Now, my problem is that I can't seem to create a new test (above the first test) that can create test data. Without having a valid given/when/then statement the test doesnt appear to be executed and calling a method from within the existing test also doesnt appear to work. I have looked into the grails-remote-control plugin to help me and I believe this will allow me to successfully envoke closures to setup data but I am not sure on the best mechanism for calling this from within the GebReportSpecs (or some abstract parent).

Below is a brief outline of the kind of thing I want to be able to do, either by making 'setupData()' the first test or by calling that method from within a test... Neither appears to work.

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

Are there any annotations like @before etc that can force these methods to be invokved?

Any suggestions are appreciated.

Solution: I have accepted dmahapatro's response below at the correct answer, but have also provided an example of my final solution below for those who may find it useful.

解决方案

(Untested)
GebReportingSpec extends GebSpec which ultimately extends spock.lang.Specification which has Fixture Methods.

You can use them like:

@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"(){

    }
}

You cannot use setup as one of your test method because the sate is not maintained for a single test case. It goes like this:-

setup called -> test1 -> teardown called  
setup called -> test2 -> teardown called  
setup called -> test3 -> teardown called  
.........

这篇关于如何设置和拆卸Geb grails中的功能测试数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆