5

I have a set of Spec tests I am executing within a Grails Project.

I need to execute a certain set of Specs when I am on local, and another set of Spec when I run the pre-prod environment. My current config is executing all my specs at the same time for both environements, which is something I want to avoid.

I have multiple environments, that I have configured in my GebConfig:

environments {
    local {
        baseUrl = "http://localhost:8090/myApp/login/auth"
    }

    pre-prod {
        baseUrl = "https://preprod/myApp/login/auth"
    }

}
Cœur
  • 37,241
  • 25
  • 195
  • 267
ErEcTuS
  • 777
  • 1
  • 14
  • 33

1 Answers1

4

You could use a spock config file.

Create annotations for the two types of tests - @Local and @PreProd, for example in Groovy:

import java.lang.annotation

@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.TYPE, ElementType.METHOD])
@Inherited
public @interface Local {}

Next step is to annotate your specs accordingly, for example:

@Local
class SpecificationThatRunsLocally extends GebSpec { ... }

Then create a SpockConfig.groovy file next to your GebConfig.groovy file with the following contents:

def gebEnv = System.getProperty("geb.env")
if (gebEnv) {
    switch(gebEnv) {
        case 'local':
            runner { include Local }
            break
        case 'pre-prod':
            runner { include PreProd }
            break 
    }
}

EDIT: It looks like Grails is using it's own test runner which means SpockConfig.groovy is not taken into account when running specifications from Grails. If you need it to work under Grails then the you should use @IgnoreIf/@Require built-in Spock extension annotations.

First create a Closure class with the logic for when a given spec should be enabled. You could put the logic directly as a closure argument to the extension annotations but it can get annoying to copy that bit of code all over the place if you want to annotate a lot of specs.

class Local extends Closure<Boolean> {
    public Local() { super(null) }
    Boolean doCall() {
        System.properties['geb.env'] == 'local'
    }
} 

class PreProd extends Closure<Boolean> {
    public PreProd() { super(null) }
    Boolean doCall() {
        System.properties['geb.env'] == 'pre-prod'
    }
}

And then annotate your specs:

@Requires(Local)
class SpecificationThatRunsLocally extends GebSpec { ... }

@Requires(PreProd)
class SpecificationThatRunsInPreProd extends GebSpec { ... }
Ted Naleid
  • 26,511
  • 10
  • 70
  • 81
erdi
  • 6,944
  • 18
  • 28
  • Thanks erdi for you response, but how do I create annotations please, do you have examples? Tx – ErEcTuS Sep 06 '13 at 15:23
  • erdi, my SockConfig is not 'seen' by my grails application..and I put it at the same package as gebConfig. While debugging, the code inside spockconfig is never reached – ErEcTuS Sep 09 '13 at 09:22
  • Is it called SpockConfig.groovy and located inside of test/functional directory of your grails project? Are you running your functional tests using `grails test-app functional:`? – erdi Sep 09 '13 at 11:40
  • Yes, actually I'm running my test from IntelliJ and the command is :test-app functional:TestSpec – ErEcTuS Sep 10 '13 at 12:39
  • is SpockConfig.groovy located in the root of test/functional? – erdi Sep 10 '13 at 12:44
  • SpockConfig.groovy is in : /grailsProject/test/functional/com/project/test/**here with all the Spec** – ErEcTuS Sep 10 '13 at 13:21
  • it should be in /grailsProject/test/functional/SpockConfig.groovy, the same as /grailsProject/test/functional/GebConfig.groovy, otherwise the files won't be picked up. – erdi Sep 10 '13 at 13:55
  • It is in the same package as GebConfig, but doesn't seem to be reached...With breakpoints, my debugger stops in GebConfig, but not in SpockConfig.. – ErEcTuS Sep 10 '13 at 16:02
  • I'm surprised that debugger stops in GebConfig.groovy that is not in the defualt package... Anyway, give moving SpockConfig.groovy to /grailsProject/test/functional a try. – erdi Sep 10 '13 at 16:08
  • erdi, it is the same as before, even in test/functional..I've tried it in another grails/Geb project, and the SpockConfig doesn't seem to be reached. Did you tried it before? – ErEcTuS Sep 11 '13 at 09:37
  • I never tried it before with Grails but it works for me in a Gradle build. It's probably because Grails has it's own runner for Spock tests. Will have a look into Grails Spock plugin to see why it's not being picked up. – erdi Sep 11 '13 at 09:50