通过gradle运行特定的@Issue

C. *_*kli 3 java groovy junit gradle spock

我正在使用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"() {
    ...
  }
}
Run Code Online (Sandbox Code Playgroud)

我想为一个问题运行所有测试(FOOBAR-123).

在rspec中很容易:

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

rspec --tag foobar-123
Run Code Online (Sandbox Code Playgroud)

但我无法看到Spock和Gradle如何做到这一点.

Tar*_*ani 5

这可以通过使用扩展来实现.您可以通过在项目中创建文件来实现

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


}
Run Code Online (Sandbox Code Playgroud)

然后在创建下面的文件

META-INF /服务/ org.spockframework.runtime.extension.IGlobalExtension

com.tarun.lalwani.IssueIncludeExtension
Run Code Online (Sandbox Code Playgroud)

之后,您可以更新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'
}
Run Code Online (Sandbox Code Playgroud)

现在我在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']
    }

}
Run Code Online (Sandbox Code Playgroud)

运行测试只运行Issue4相关测试

根据问题过滤