Spock @IgnoreIf基于属性文件

时间:2016-07-26 06:56:25

标签: java groovy spock ignore skip

我想通过从属性文件中获取值然后输入IgnoreIf谓词来忽略测试。如果有可能请告诉我。如果没有,请帮助我解决方法。

提前致谢。

1 个答案:

答案 0 :(得分:0)

Spock Manual, chapter "Extensions"描述了如何使用sysenvosjvm等绑定变量。但基本上你可以在那里放置任何Groovy闭包。

如果在运行测试时在命令行上指定环境变量或系统属性,则可以使用envsys来访问它们。但是如果你绝对想要从文件中读取属性,只需使用这样的辅助类:

文件 spock.properties

如果您使用Maven构建,可能您希望将文件放在 src / test / resources 下的某个位置。

spock.skip.slow=true

Helper类读取属性文件:

class SpockSettings {
    public static final boolean SKIP_SLOW_TESTS = ignoreLongRunning();

    public static boolean ignoreLongRunning() {
        def properties = new Properties()
        def inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("spock.properties")
        properties.load(inputStream)
        inputStream.close()
        //properties.list(System.out)
        Boolean.valueOf(properties["spock.skip.slow"])
    }
}

使用帮助程序类进行测试:

import spock.lang.IgnoreIf
import spock.lang.Specification
import spock.util.environment.OperatingSystem

class IgnoreIfTest extends Specification {
    @IgnoreIf({ SpockSettings.SKIP_SLOW_TESTS })
    def "slow test"() {
        expect:
        true
    }

    def "quick test"() {
        expect:
        true
    }

    @IgnoreIf({ os.family != OperatingSystem.Family.WINDOWS })
    def "Windows test"() {
        expect:
        true
    }

    @IgnoreIf({ !jvm.isJava8Compatible() })
    def "needs Java 8"() {
        expect:
        true
    }

    @IgnoreIf({ env["USERNAME"] != "kriegaex" })
    def "user-specific"() {
        expect:
        true
    }
}