Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Executing Specific Geb Tests according to environment

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

}
like image 751
ErEcTuS Avatar asked Oct 03 '22 01:10

ErEcTuS


1 Answers

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 { ... }
like image 68
erdi Avatar answered Oct 12 '22 12:10

erdi