1
votes

Is there a easy way in Geb/Spock to ensure a login happens before all functional tests?

For instance my login test looks like

def "login"() {
        when:
            to Login
        and:
            login(username,password)
        then:
            at Dashboard
        where:
            username   | password
            "X"        | "X"
    }

This is quite alot of code to put into each other test.

2
Perhaps stackoverflow.com/questions/13575972/… is the answer. Basically runs tests in order... but still doesn't seem the ideal solution. - IanWatson
Depending on test order is often not a good idea. It may lead to test bleed and strange interdependencies where failure in one test may cause failure in another seemingly independent test. Suites that dependent on implicit state setup by tests are brittle and hard to reason about and maintain. - erdi

2 Answers

2
votes

Create an abstract base spec which all of your specs that require logging in can extend:

abstract class LoginBaseSpec extends GebReportingSpec{

  def setupSpec(){
    when:
      to Login

    and:
      login(username, password)

    then:
      at Dashboard  
  }

This setupSpec() method in the super spec will be executed before anything in the extending specs.

1
votes

Putting the common login code in a setup() method of a base spec class as proposed by jk47 is one way to go about it.

Another one, which doesn't tie you to a specific inheritance structure, is to use a JUnit rule which are supported by Spock out of the box.