def "testing somthing"() {
expect:
assert myService.getResult(x) == y
where:
x | y
5 | 7
10 | 12
}
注释:
@Unroll
它将为您创建 10 个单独的测试。
编辑问题后进行编辑,使用最简单的方法来实现此目的:
@Unroll("test repeated #i time")
def "test repeated"() {
expect:
println i
where:
i << (1..10)
}
}
这是目前在 spock 中执行此操作的唯一方法。
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.lang.annotation.ElementType
import java.lang.annotation.Target
import org.spockframework.runtime.extension.ExtensionAnnotation
import org.spockframework.runtime.extension.AbstractAnnotationDrivenExtension
import org.spockframework.runtime.model.FeatureInfo
import org.spockframework.runtime.extension.AbstractMethodInterceptor
import org.spockframework.runtime.extension.IMethodInvocation
@Retention(RetentionPolicy.RUNTIME)
@Target([ElementType.METHOD, ElementType.TYPE])
@ExtensionAnnotation(RepeatExtension.class)
public @interface Repeat {
int value() default 1;
}
public class RepeatExtension extends AbstractAnnotationDrivenExtension<Repeat> {
@Override
public void visitFeatureAnnotation(Repeat annotation, FeatureInfo feature) {
feature.addInterceptor(new RepeatInterceptor(annotation.value()));
}
}
private class RepeatInterceptor extends AbstractMethodInterceptor{
private final int count;
public RepeatInterceptor(int count) {
this.count = count;
}
@Override
public void interceptFeatureExecution(IMethodInvocation invocation) throws Throwable {
for (int i = 0; i < count; i++) {
invocation.proceed();
}
}
}
def "testing somthing"() {
expect:
assert myService.getResult(x) == y
where:
[x,y] << getMap()
}
public ArrayList<Integer[]> getMap(){
ArrayList<Integer[]> toReturn = new ArrayList<Integer[]>();
for(int i = 0; i < 5; i ++){
toReturn.add({5, 7})
toReturn.add({10, 12})
}
return toReturn;
}
def "testing somthing"() {
expect:
assert myService.getResult(x) == y
where:
x >> [3,5,7]
y >> [5,10,12]
}
注释您的测试,启动后它将被重复调用。