2016-07-26 29 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 
    } 
}