根据环境执行特定的Geb测试 [英] Executing Specific Geb Tests according to environment

查看:90
本文介绍了根据环境执行特定的Geb测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Grails项目中执行一组规范测试.

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.

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

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

}

推荐答案

您可以使用spock配置文件.

You could use a spock config file.

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

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 { ... }

然后在您的GebConfig.groovy文件旁边创建一个SpockConfig.groovy文件,其内容如下:

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

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

首先创建一个Closure类,其中包含何时启用给定规范的逻辑.您可以将逻辑直接作为扩展注释的闭包参数,但是如果您要注释很多规范,可能会在整个位置复制该段代码,这会很烦人.

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

然后注释您的规格:

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

@Requires(PreProd)
class SpecificationThatRunsInPreProd extends GebSpec { ... }

这篇关于根据环境执行特定的Geb测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆