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"
}
}
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 { ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With