2016-07-26 83 views
0

我想通过从属性文件中获取值并将其提供给IgnoreIf谓词来忽略测试。让我知道是否有可能。如果不是,请使用解决方法帮助我。Spock @IgnoreIf基于属性文件

在此先感谢。

回答

0

Spock Manual, chapter "Extensions",介绍了如何使用绑定变量一样sysenvosjvm。但基本上,您可以将任何Groovy闭包放在那里。

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

文件spock.properties

也许你想要把文件保存在某个下的src /如果您使用Maven构建测试/资源

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 
    } 
}