Tianjiao Zhu
Tianjiao Zhu

Reputation: 13

How to run Spock spec multiple times with different parameters?

I am writing automation test scripts using Groovy+Geb+Spock+Gradle framework. I have a spec called "UserProfilePageSpec" to check UI of user profile page and now I want to run it multiple times to check multiple users. I added randomness there so that each time the spec will select a random user in the list and navigate to the user profile page.

Also, I want to check 5 users per user type (which is a filter in homepage). So I need to click on each user type in the homepage, and then run the spec multiple times.

Sometimes I need to test just one user when there is no enough time to test multiple users in each type, so I kinda want to keep this spec to test for one user itself, does anyone have a good idea of implementing this? Thanks!

Here is a basic structure of "UserProfilePageSpec":

class UserProfilePageSpec {
    def setupSpec(){
        //login
        //select a random user
        //navigate to user profile page
    }
    def "test1"(){..}
    def "test2"(){..}
    def "test3"(){..}
    def "test4"(){..}
}

Upvotes: 1

Views: 2015

Answers (1)

Leonard Brünings
Leonard Brünings

Reputation: 13222

This is not possible in Spock, there are some open issues for it, e.g. https://github.com/spockframework/spock/issues/407 or https://github.com/spockframework/spock/issues/668

Depending on you usecase, you can use subclasses to simulate that.

abstract class UserProfilePageSpec {
    def setupSpec(){
        //login
        //select a random user
        //navigate to user profile page
    }

    abstract def getData()

    def "test1"(){..}
    def "test2"(){..}
    def "test3"(){..}
    def "test4"(){..}
}

class UserProfilePageVariation1Spec extends UserProfilePageSpec {

    def getData() { [username: 'foo', password: 'bar'] } 
}

Just create your variations as subclasses.

Upvotes: 5

Related Questions