grails - 标记Geb类别/测试以排除

标签 grails spock geb

我正在尝试创建一种简单的方法,因为没有可选的“标签”,因此无法运行Geb测试。

在我的grails项目中,我为我的geb文件创建了一个“test / functional”目录。在此目录中,我有GebConfig.groovy和SpockConfig.groovy文件。我知道SpockConfig.groovy当前在解析项目中的类时遇到了一个问题,只能识别BuildConfig依赖项中提供的类。这样,我创建并添加了一个注释类“Tags”,并将其包含在依赖项中。现在,我可以通过以下方式在我的Geb测试中使用此批注:

@Tags("regression")
@Tags(["smoke", "regression"])
@Tags(["in-progress", "whatever-makes-sense"])

我现在想要的是让SpockConfig在带有@Tags批注的“test / functional”目录中找到所有类,并为每个没有在System.getenv(“functional.tag”)中的类标签数组,将类添加到Runner.exclude基类集合中,因此它不会执行。

我对Annotations及其处理非常陌生,到目前为止,还没有找到执行此过程的查找逻辑的简便方法。是否存在促进这种过程的现有结构,或者暗含什么逻辑来完成此任务?

编辑:

我可以通过以下类(class)实现此功能...
public class IgnoreUnlessExtension extends AbstractAnnotationDrivenExtension<IgnoreUnless> {
    private static final Pattern JAVA_VERSION = Pattern.compile("(\\d+\\.\\d+).*");

    private static final Object DELEGATE = new Object() {
        public Map<String, String> getEnv() {
            return System.getenv();
        }

        public Properties getProperties() {
            return System.getProperties();
        }

        public BigDecimal getJavaVersion() {
            String versionString = System.getProperty("java.version");
            Matcher matcher = JAVA_VERSION.matcher(versionString);
            if (matcher.matches()) return new BigDecimal(matcher.group(1));
            throw new InternalSpockError(versionString);
        }
    };

    @Override
    public void visitSpecAnnotation(IgnoreUnless annotation, SpecInfo spec) {
        doVisit(annotation, spec);
    }

    @Override
    public void visitFeatureAnnotation(IgnoreUnless annotation, FeatureInfo feature) {
        doVisit(annotation, feature);
    }

    private void doVisit(IgnoreUnless annotation, ISkippable skippable) {
        String[] tags = annotation.value();
        String targetTag = System.getenv("functional.tag");

        if (GroovyRuntimeUtil.isTruthy(targetTag)) {
            skippable.setSkipped(!tags.contains(targetTag));
        }
    }
}

@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.TYPE, ElementType.METHOD])
@ExtensionAnnotation(IgnoreUnlessExtension.class)
public @interface IgnoreUnless {
    String[] value() default [];
}

现在,使用@IgnoreUnless批注,我可以将类或方法标记为@IgnoreUnless(“tag1”)或@IgnoreUnless([“tag1”,“tag2”])),并且只要在run命令上提供了标签,就可以对测试进行注释并且其列表中没有提供的标签将被跳过。

最佳答案

要跳过带有注释的规范,您可以进行扩展以实现Spock的IAnnotationDrivenExtension。有关示例,请参见IgnoreExtension和IgnoreIfExtension。您的扩展程序可以获取“functional.tag”环境变量,并在“标签”注释的值中进行搜索。

如果您不介意使用多个注释,而不是参数化单个注释,则可以使用现有扩展名和配置执行所需的操作,而不用创建自己的扩展名。

但是,如果要跳过没有单个参数化批注的规范,则需要实现IGlobalExtension。有关示例,请参见IncludeExcludeExtension。您可以在jar中发布全局扩展作为服务,例如,使用Grails脚本,如下所示:

includeTargets << grailsScript('Init')
includeTargets << grailsScript('Compile')

target(buildAll: 'build the foo-spock-extensions.jar') {
    depends(compile, buildJar)
}

target(buildJar: 'build the foo-spock-extensions.jar') {
    def libDir = new File((File)grailsSettings.baseDir, 'lib')
    def jarFile = new File(libDir, 'foo-spock-extensions.jar')

    def serviceType = 'org.spockframework.runtime.extension.IGlobalExtension'
    ant.delete(quiet: true, file: jarFile)
    ant.jar( destfile: jarFile ) {
        fileset(dir: classesDirPath) {
            include(name: '**/*ExecuteWhenPropertySet*.class')  // 3 top classes + a couple closures
            include(name: '**/ReportConnectionLeaksExtension*.class')  // top class & nested Listener
        }
        // Generate the serviceType file in META-INF/services, to plugin to Spock's ExtensionClassesLoader.
        service(type: serviceType) {
            provider(classname: 'com.example.foo.testing.OnlyExecuteWhenPropertySetExtension')
            provider(classname: 'com.example.foo.testing.ReportConnectionLeaksExtension')
        }
    }
}


setDefaultTarget( buildAll )

关于grails - 标记Geb类别/测试以排除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29127169/

相关文章:

grails-2.3.0.M1无法在IntelliJ IDEA 130.754中启动

grails - 如何将 Grails 2.1 迁移到 Grails 2.3 应用程序?

grails - 如何在 Geb grails 中设置和拆卸功能测试数据

grails - 如何在GEB中选择标签文本?

gradle - 可以自定义gradle的测试报告吗? (将 geb 报告与 gradle 测试报告集成)

grails - chrome中的Geb单选按钮不起作用

mongodb - 查询模型的嵌入式列表

grails - Canoo 和 Groovy - 如何使用 storeRegEx

eclipse - Eclipse 中junit 的等宽字体已弃用

java - 是否可以覆盖对注入(inject)器的调用?