根据环境执行特定的Geb测试

ErE*_*TuS 5 testing grails spock geb

我有一组我在Grails项目中执行的Spec测试.

当我在本地时,我需要执行一组规范,当我运行pre-prod环境时,我需要另一组Spec.我当前的配置同时为两个环境执行我的所有规格,这是我想要避免的.

我有多个环境,我在GebConfig中配置了:

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

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

}
Run Code Online (Sandbox Code Playgroud)

erd*_*rdi 4

您可以使用 spock 配置文件。

为两种类型的测试创建注释 -@Local@PreProd,例如在 Groovy 中:

import java.lang.annotation

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

下一步是相应地注释您的规格,例如:

@Local
class SpecificationThatRunsLocally extends GebSpec { ... }
Run Code Online (Sandbox Code Playgroud)

SpockConfig.groovy然后在您的文件旁边创建一个GebConfig.groovy包含以下内容的文件:

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

编辑:看起来 Grails 正在使用它自己的测试运行器,这意味着在运行 Grails 规范时不会考虑 SpockConfig.groovy。如果您需要它在 Grails 下工作,那么您应该使用 @IgnoreIf/@Require 内置 Spock 扩展注释。

首先创建一个 Closure 类,其中包含何时启用给定规范的逻辑。您可以将逻辑直接作为扩展注释的闭包参数,但如果您想注释大量规范,则将这段代码复制到各处可能会很烦人。

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'
    }
}
Run Code Online (Sandbox Code Playgroud)

然后注释你的规格:

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

@Requires(PreProd)
class SpecificationThatRunsInPreProd extends GebSpec { ... }
Run Code Online (Sandbox Code Playgroud)