Groovy 当我检查变量时,如何在Spock中使用IgnoreIf?

Groovy 当我检查变量时,如何在Spock中使用IgnoreIf?,groovy,spock,Groovy,Spock,我需要跳过程序中的一些函数,但它应该取决于同一程序中定义的变量。我怎么做 def skip = true @IgnoreIf({ skip }) def "some function" () { .. } 您可以通过在静态上下文中访问skip字段来执行此操作: import spock.lang.IgnoreIf import spock.lang.Specification class IgnoreIfSpec extends Specification { static bo

我需要跳过程序中的一些函数,但它应该取决于同一程序中定义的变量。我怎么做

def skip = true

@IgnoreIf({ skip })
def "some function" () {
..
}

您可以通过在静态上下文中访问
skip
字段来执行此操作:

import spock.lang.IgnoreIf
import spock.lang.Specification

class IgnoreIfSpec extends Specification {

    static boolean skip = true

    @IgnoreIf({ IgnoreIfSpec.skip })
    def "should not execute this test if `IgnoreIfSepc.skip` is set to TRUE"() {
        when:
        def res = 1 + 1

        then:
        res == 2
    }

    def "should execute this test every time"() {
        when:
        def res = 1 + 1

        then:
        res == 2
    }
}

否则,传递给
@IgnoreIf()
的闭包尝试在闭包中查找
跳过
字段,但失败。

您可以通过在静态上下文中访问
跳过
字段来完成此操作:

import spock.lang.IgnoreIf
import spock.lang.Specification

class IgnoreIfSpec extends Specification {

    static boolean skip = true

    @IgnoreIf({ IgnoreIfSpec.skip })
    def "should not execute this test if `IgnoreIfSepc.skip` is set to TRUE"() {
        when:
        def res = 1 + 1

        then:
        res == 2
    }

    def "should execute this test every time"() {
        when:
        def res = 1 + 1

        then:
        res == 2
    }
}

否则,传递到
@IgnoreIf()
的闭包尝试在闭包中查找
跳过
字段,但失败。

另一种方法是使用的配置文件包括/排除测试或基类

首先,您创建自己的注释并将其放在测试上。
然后编写一个Spock配置文件。
运行测试时,可以将
spock.configuration
属性设置为您选择的配置文件。
通过这种方式,您可以包括/排除所需的测试和基类

spock配置文件示例:

runner {
    println "Run only tests"
    exclude envA
}
您的测试:

@envA
def "some function" () {
    ..
}
然后,您可以运行:

./gradlew test -Pspock.configuration=mySpockConfig.groovy

另一种方法是使用的配置文件包括/排除测试或基类

首先,您创建自己的注释并将其放在测试上。
然后编写一个Spock配置文件。
运行测试时,可以将
spock.configuration
属性设置为您选择的配置文件。
通过这种方式,您可以包括/排除所需的测试和基类

spock配置文件示例:

runner {
    println "Run only tests"
    exclude envA
}
您的测试:

@envA
def "some function" () {
    ..
}
然后,您可以运行:

./gradlew test -Pspock.configuration=mySpockConfig.groovy

这里是一个

如果需要首先计算变量,然后根据计算结果做出忽略测试的决定,我们可以使用静态块和静态变量

import spock.lang.IgnoreIf
import spock.lang.Specification

class IgnoreIfSpec extends Specification {

    static final boolean skip

    static {
    //some code for computation
    if("some condition")
       skip = true
    else
       skip = false
   }

    @IgnoreIf({ IgnoreIfSpec.skip })
    def "should not execute this test if `IgnoreIfSepc.skip` is set to TRUE"() {
        when:
        def res = 1 + 1

        then:
        res == 2
    }

    def "should execute this test every time"() {
        when:
        def res = 1 + 1

        then:
        res == 2
    }
}

如果需要先计算变量,然后在计算的基础上做出忽略测试的决定,我们可以使用静态块和静态变量

import spock.lang.IgnoreIf
import spock.lang.Specification

class IgnoreIfSpec extends Specification {

    static final boolean skip

    static {
    //some code for computation
    if("some condition")
       skip = true
    else
       skip = false
   }

    @IgnoreIf({ IgnoreIfSpec.skip })
    def "should not execute this test if `IgnoreIfSepc.skip` is set to TRUE"() {
        when:
        def res = 1 + 1

        then:
        res == 2
    }

    def "should execute this test every time"() {
        when:
        def res = 1 + 1

        then:
        res == 2
    }
}

酷,我很高兴能帮助你:)嗯,还有一个更重要的问题。如何更改测试中的
跳过
?我想创建测试,将
skip
设置为true/false,这样如果条件没有实现,其他测试就不会启动。我不建议这样做。您不能保证测试运行的顺序,并且有一个原则,即每个单元测试都应该在隔离状态下运行。你想解决的问题是什么?也许有更好的方法解决这个问题?顺便说一句,有一个
@逐步的
注释可以添加到测试类中-它强制spock按顺序运行该类中的所有测试,如果一个测试失败,则忽略所有其他测试。也许这是一个值得尝试的解决方案。它迫使您将所有依赖的测试放在一个类中,但这种情况并不常见。您应该始终将您的测试视为一个单独的单元,它不需要执行任何其他测试,也不依赖于任何其他测试的执行。此外,更新类外的任何静态值也是您应该避免的事情,如fire—这通常是一种设计差的味道。我希望这有助于找到解决你问题的最佳办法。祝你好运酷,我很高兴能帮助你:)嗯,还有一个更重要的问题。如何更改测试中的
跳过
?我想创建测试,将
skip
设置为true/false,这样如果条件没有实现,其他测试就不会启动。我不建议这样做。您不能保证测试运行的顺序,并且有一个原则,即每个单元测试都应该在隔离状态下运行。你想解决的问题是什么?也许有更好的方法解决这个问题?顺便说一句,有一个
@逐步的
注释可以添加到测试类中-它强制spock按顺序运行该类中的所有测试,如果一个测试失败,则忽略所有其他测试。也许这是一个值得尝试的解决方案。它迫使您将所有依赖的测试放在一个类中,但这种情况并不常见。您应该始终将您的测试视为一个单独的单元,它不需要执行任何其他测试,也不依赖于任何其他测试的执行。此外,更新类外的任何静态值也是您应该避免的事情,如fire—这通常是一种设计差的味道。我希望这有助于找到解决你问题的最佳办法。祝你好运