运行 通过 gradle 的特定@Issue

Running a specific @Issue via gradle

我正在使用 Spock 编写测试并 Gradle 到 运行 它们。我使用 @Issue 注释它们。同一问题的测试不一定在同一个文件中:

FooTest.groovy:

class FooTest extends Specification {
  @Issue("FOOBAR-123")
  def "should foo"() {
    ...
  }
  @Issue("FOOBAR-234")
  def "should bar"() {
    ...
  }
}

BarTest.groovy:

class BarTest extends Specification {
  @Issue("FOOBAR-123")
  def "should quux"() {
    ...
  }
}

我想 运行 对单个问题 (FOOBAR-123) 进行所有测试。

在 rspec 中很容易:

describe 'foo' do
  it "should foo", foobar-123: true do
    ...
  end

rspec --tag foobar-123

但我看不出如何使用 Spock 和 Gradle。

根据this文章,您可以为任何注释创建配置。

不幸的是(经过一些尝试)我无法根据注释的值过滤测试,IncludeExcludeCriteria class 只接受 classes 或注释。

runner {
  include spock.lang.Issue
}

我认为您可以通过为每个问题创建注释并使用 Annotations Collectors.

来解决这个问题

这可以通过使用扩展来实现。您可以通过在项目中创建一个文件来做到这一点

IssueIncludeExtension.groovy

package com.tarun.lalwani

import org.spockframework.runtime.extension.AbstractGlobalExtension
import org.spockframework.runtime.model.FeatureInfo
import org.spockframework.runtime.model.SpecInfo
import spock.lang.Issue

import java.lang.annotation.Annotation

class IssueIncludeExtension extends AbstractGlobalExtension{
    @Override
    void visitSpec(SpecInfo spec) {
        def issuesList
        issuesList = System.properties["spock.issues"]

        if (issuesList) {
            def arrIssues = issuesList.split(",").toList()

            System.out.println('I was called')
            for (FeatureInfo feature : spec.getAllFeatures())
            {
                def method, ann;
                method = feature.getFeatureMethod();
                def issueAnnotation = method.getAnnotation(Issue.class);
                if (issueAnnotation) {
                    if (issueAnnotation.value().size() > 0)
                    {
                        if (issueAnnotation.value().toList().intersect(arrIssues))
                        {
                            //we have a matching issue
                            feature.setExcluded(false)
                        } else {
                            feature.setExcluded((true))
                        }


                    }
                } else {
                    // this doesn't belong to any issue
                    feature.setExcluded(true)
                }
            }

        } else {
            super.visitSpec(spec)
        }
    }


}

然后在下面创建文件

META-INF/services/org.spockframework.runtime.extension.IGlobalExtension

com.tarun.lalwani.IssueIncludeExtension

之后您可以更新 gradle 脚本并添加测试

task issueTest(type: Test) {
    // This task belongs to Verification task group.
    group = 'Verification'

    // Set Spock configuration file when running
    // this test task.
    systemProperty 'spock.issues', 'Issue4'
}

现在我在 groovy

中进行了以下测试
package com.mrhaki.spock

import spock.lang.Issue
import spock.lang.Specification

class WordRepositorySpec extends Specification {


    @Remote  // Apply our Remote annotation.
    @Issue(["Issue1", "Issue2"])
    def "test remote access"() {
        given:
        final RemoteAccess access = new RemoteAccess()

        expect:
        access.findWords('S') == ['Spock']
    }

    @Issue("Issue4")
    def "test local access"() {
        given:
        final LocalAccess access = new LocalAccess()

        expect:
        access.findWords('S') == ['Spock']
    }

}

运行 测试刚刚运行 Issue4 相关测试