如上所述,目前还不支持here @Repeat注释。如何将spock测试标记为重复n次?
假设我有spock测试:
def "testing somthing"() {
expect:
assert myService.getResult(x) == y
where:
x | y
5 | 7
10 | 12
}我如何才能将其标记为重复n次?
发布于 2012-06-06 19:54:15
您可以使用where-block,如上面的答案所示。目前没有办法重复已经有where块的方法。
发布于 2012-06-06 15:48:55
您可以像这样使用@Unroll注释:
@Unroll("test repeated #i time")
def "test repeated"() {
expect:
println i
where:
i << (1..10)
}它将为您创建10个单独的测试。
编辑编辑您的问题之后,使用最简单的方法来实现此目的:
def "testing somthing"() {
expect:
assert myService.getResult(x) == y
where:
x | y
5 | 7
5 | 7
5 | 7
5 | 7
5 | 7
10 | 12
10 | 12
10 | 12
10 | 12
10 | 12}
这是目前在spock中实现这一点的唯一方法。
发布于 2012-06-06 20:26:35
我已经找到了有效的解决方案here (ru)
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();
}
}
}https://stackoverflow.com/questions/10910080
复制相似问题