这可以通过使用扩展来实现。你可以通过在你的项目中创建一个文件来做到这一点
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相关测试