Executing Specific Geb Tests according to environment

牧云@^-^@ 提交于 2019-12-03 00:39:28
erdi

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 { ... }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!