Unit testing 从Spock测试中动态排除类
我们希望从Spock测试中排除一组特定的测试类,这取决于是否设置了某些系统属性。现在我们得到了一些代码,比如:Unit testing 从Spock测试中动态排除类,unit-testing,groovy,configuration,spock,Unit Testing,Groovy,Configuration,Spock,我们希望从Spock测试中排除一组特定的测试类,这取决于是否设置了某些系统属性。现在我们得到了一些代码,比如: runner { // skip all slow tests automatically unless test.include.slow=true if (!Boolean.getBoolean('test.include.slow')) { exclude Slow } // skip all api tests unless test.include.
runner {
// skip all slow tests automatically unless test.include.slow=true
if (!Boolean.getBoolean('test.include.slow')) { exclude Slow }
// skip all api tests unless test.include.api=true
if (!Boolean.getBoolean('test.include.api')) { exclude ApiTest }
}
在我们的SpockConfig.groovy中。问题是,第二次调用exclude实际上覆盖了第一次调用中定义的excludes。我们还尝试构建一个类数组,并将其处理到exclude函数,如下所示:
runner {
Class[] toExclude = []
// skip all slow tests automatically unless test.include.slow=true
if (!Boolean.getBoolean('test.include.slow')) { toExclude << Slow }
// skip all api tests unless test.include.api=true
if (!Boolean.getBoolean('test.include.api')) { toExclude << ApiTest }
exclude toExclude
}
runner{
类[]排除=[]
//自动跳过所有慢速测试,除非test.include.slow=true
如果(!Boolean.getBoolean('test.include.slow'){toExcludeOk,我们找到了一些解决方案:
runner {
// skip all slow tests automatically unless test.include.slow=true
if (!Boolean.getBoolean('test.include.slow')) { exclude.annotations << Slow }
// skip all api tests unless test.include.api=true
if (!Boolean.getBoolean('test.include.api')) { exclude.annotations << ApiTest }
}
runner{
//自动跳过所有慢速测试,除非test.include.slow=true
如果(!Boolean.getBoolean('test.include.slow'){exclude.annotationsOk,我们找到了一些解决方案:
runner {
// skip all slow tests automatically unless test.include.slow=true
if (!Boolean.getBoolean('test.include.slow')) { exclude.annotations << Slow }
// skip all api tests unless test.include.api=true
if (!Boolean.getBoolean('test.include.api')) { exclude.annotations << ApiTest }
}
runner{
//自动跳过所有慢速测试,除非test.include.slow=true
if(!Boolean.getBoolean('test.include.slow'){exclude.annotationsSpock现在有了@IgnoreIf
注释来完成这项工作
在您希望排除的每个测试之上,您可以注释条件,包括系统属性、环境变量和java版本信息
@IgnoreIf({ !Boolean.valueOf(properties['test.include.slow']) })
def "run spec if Java system property 'test.include.slow' is true"() {
expect:
true
}
斯波克现在有了@IgnoreIf
注释来完成这项工作
在您希望排除的每个测试之上,您可以注释条件,包括系统属性、环境变量和java版本信息
@IgnoreIf({ !Boolean.valueOf(properties['test.include.slow']) })
def "run spec if Java system property 'test.include.slow' is true"() {
expect:
true
}
Spock支持JUnit测试规则,它可以提供一种非常干净的方法来实现这一点。例如,您的功能可以只使用注释来指定何时运行:
@SlowTest
def "run spec if Java system property 'test.include.slow' is true"() {
expect:
true
}
@ApiTest
def "run spec if Java system property 'test.include.api' is true"() {
expect:
true
}
首先,您需要创建注释:
SlowTest.java:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SlowTest {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiTest {
}
public class AnnotationsTestRule implements TestRule {
private final boolean mApiTest;
private final boolean mSlowTest;
public AnnotationsTestRule() {
mApiTest = Boolean.getBoolean("test.include.api")
mSlowTest = Boolean.getBoolean("test.include.slow")
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (description.getAnnotation(ApiTest.class) != null && !mApiTest || description.getAnnotation(SlowTest.class) != null && !mSlowTest) {
//Skip test when the annotation is present but not the correlating system property
return;
}
base.evaluate();
}
}
}
}
ApiTest.java:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SlowTest {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiTest {
}
public class AnnotationsTestRule implements TestRule {
private final boolean mApiTest;
private final boolean mSlowTest;
public AnnotationsTestRule() {
mApiTest = Boolean.getBoolean("test.include.api")
mSlowTest = Boolean.getBoolean("test.include.slow")
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (description.getAnnotation(ApiTest.class) != null && !mApiTest || description.getAnnotation(SlowTest.class) != null && !mSlowTest) {
//Skip test when the annotation is present but not the correlating system property
return;
}
base.evaluate();
}
}
}
}
接下来,创建测试规则:
AnnotationsTestRule.java:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SlowTest {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiTest {
}
public class AnnotationsTestRule implements TestRule {
private final boolean mApiTest;
private final boolean mSlowTest;
public AnnotationsTestRule() {
mApiTest = Boolean.getBoolean("test.include.api")
mSlowTest = Boolean.getBoolean("test.include.slow")
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (description.getAnnotation(ApiTest.class) != null && !mApiTest || description.getAnnotation(SlowTest.class) != null && !mSlowTest) {
//Skip test when the annotation is present but not the correlating system property
return;
}
base.evaluate();
}
}
}
}
最后,在每个需要注释的功能上添加本答案第一部分中提到的注释。对于使用这些注释的规范,还需要声明测试规则。只需将这一行添加到groovy文件中即可:
@Rule AnnotationsTestRule mAnnotationsTestRule
Spock支持JUnit测试规则,它可以提供一种非常干净的方法来实现这一点。例如,您的功能可以只使用注释来指定何时运行:
@SlowTest
def "run spec if Java system property 'test.include.slow' is true"() {
expect:
true
}
@ApiTest
def "run spec if Java system property 'test.include.api' is true"() {
expect:
true
}
首先,您需要创建注释:
SlowTest.java:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SlowTest {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiTest {
}
public class AnnotationsTestRule implements TestRule {
private final boolean mApiTest;
private final boolean mSlowTest;
public AnnotationsTestRule() {
mApiTest = Boolean.getBoolean("test.include.api")
mSlowTest = Boolean.getBoolean("test.include.slow")
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (description.getAnnotation(ApiTest.class) != null && !mApiTest || description.getAnnotation(SlowTest.class) != null && !mSlowTest) {
//Skip test when the annotation is present but not the correlating system property
return;
}
base.evaluate();
}
}
}
}
ApiTest.java:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SlowTest {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiTest {
}
public class AnnotationsTestRule implements TestRule {
private final boolean mApiTest;
private final boolean mSlowTest;
public AnnotationsTestRule() {
mApiTest = Boolean.getBoolean("test.include.api")
mSlowTest = Boolean.getBoolean("test.include.slow")
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (description.getAnnotation(ApiTest.class) != null && !mApiTest || description.getAnnotation(SlowTest.class) != null && !mSlowTest) {
//Skip test when the annotation is present but not the correlating system property
return;
}
base.evaluate();
}
}
}
}
接下来,创建测试规则:
AnnotationsTestRule.java:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SlowTest {
}
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiTest {
}
public class AnnotationsTestRule implements TestRule {
private final boolean mApiTest;
private final boolean mSlowTest;
public AnnotationsTestRule() {
mApiTest = Boolean.getBoolean("test.include.api")
mSlowTest = Boolean.getBoolean("test.include.slow")
}
@Override
public Statement apply(Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
if (description.getAnnotation(ApiTest.class) != null && !mApiTest || description.getAnnotation(SlowTest.class) != null && !mSlowTest) {
//Skip test when the annotation is present but not the correlating system property
return;
}
base.evaluate();
}
}
}
}
最后,在每个需要注释的功能上添加本答案第一部分中提到的注释。对于使用这些注释的规范,还需要声明测试规则。只需将这一行添加到groovy文件中即可:
@Rule AnnotationsTestRule mAnnotationsTestRule
我不知道这是否是推荐的方法,但是我想补充一点,您可以这样指定基类:'exclude.baseClasses我不知道这是否是推荐的方法,但是我想补充一点,您可以这样指定基类:'exclude.baseClasses我在博客中找到它,并且有更详细的内容信息:我在这里找到的博客有更详细的信息: