How to set and cancel functional test data in Geb grails

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

I want to move the creation of test data to the startup () / teardown () methods inside each Spec, well, actually, I wanted them to inherit it from the base class, but obviously, StepWise has problems with inheritance.

So, currently, each of my test class classes looks something like this:

@Stepwise class ExampleSpec extends GebReportingSpec { def "valid root user logs in"() { given: "I am at the login page" to LoginPage when: "I enter root 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 cannot create a new test (above the first test) that can create test data. Without a valid given / if / then test, the test seems to fail, and calling the method from the existing test also fails. I looked into the grails-remote-control plugin to help me, and I believe that this will allow me to successfully declare a close for data installation, but I'm not sure of the best mechanism to call this from GebReportSpecs (or some abstract parent).

Below is a brief overview of what I want to do, either by making "setupData ()" the first test, or by calling this method from the test ... Nothing works.

 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 cause these methods to be called?

Any suggestions are welcome.

Solution: I accepted the dmahapatro answer below with the correct answer, but also provided an example of my final solution below for those who may find this useful.

+4
source share
2 answers

(untested)
GebReportingSpec extends GebSpec , which ultimately extends spock.lang.Specification which has spock.lang.Specification Methods .

You can use them as:

 @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 the installation as one of your test methods, because the satellite is not supported for one test case. This happens as follows: -

 setup called -> test1 -> teardown called setup called -> test2 -> teardown called setup called -> test3 -> teardown called ......... 
+3
source

## Solved ##

Thanks dmahapatro (and erdi). I specifically masked setupSpec () and cleanup () since they are private in GebReportingSpec.

Just for the sake of completion, I am going to publish a simplified version of my final solution using the Grails plugin plugin , anyway it will help someone else. The only thing to note is that setup / breaks are called once per Spec, and not before each test. For me, this is really desirable, since my test data is quite complex and takes time to create. Thus, you have a test data set from Spec, which is modified using tests in Spec, and then finally cleared before the next Spec starts.

 @Stepwise class TestDataBaseSpec extends GebReportingSpec { protected void createTestUsers() { def remote = new RemoteControl() def created = remote { def createUser = { name, roles, pwHash -> def user = new ShiroUser(username: name, passwordHash: pwHash, passwordSetDate: new Date()) roles.each { user.addToRoles(it) } user.save(failOnError: true) return user } createUser("root", [ShiroRole.findByName("base_user")], pwHash) // .. repeat for more } } protected void deleteTestUsers() { def remote = new RemoteControl() def created = remote { ShiroUser.findAll().each { it.delete(flush: true) } return true } } } 

 @Stepwise class ExampleSpec extends TestDataBaseSpec { def setupSpec() { super.createTestUsers() } def cleanupSpec() { super.deleteTestUsers() } def "valid root user logs in"() { given: "I am at the login page" to LoginPage when: "I enter root 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 } } 
+1
source

Source: https://habr.com/ru/post/1486627/


All Articles