Automated tests 斯波克';s@IgnoreIf闭包不';t查看jUnit'中设置的系统属性变量;s套房

Automated tests 斯波克';s@IgnoreIf闭包不';t查看jUnit'中设置的系统属性变量;s套房,automated-tests,spock,geb,test-suite,junit-runner,Automated Tests,Spock,Geb,Test Suite,Junit Runner,我使用Geb+Spock+junitrunner+Maven 我的规格如下: @Stepwise class My_Spec extends GebReportingSpec { @IgnoreIf({properties['sss'].contains('true')}) def "testFeature 1" (){ println ("--> Feature 1 runs") given: println ("--&

我使用Geb+Spock+junitrunner+Maven 我的规格如下:

@Stepwise
class My_Spec extends GebReportingSpec {

    @IgnoreIf({properties['sss'].contains('true')})

    def "testFeature 1" (){
        println ("--> Feature 1 runs")
        given:
        println ("--> mySystemProp is: ${properties['sss']}")
        ...
        when:
        ...
        then:
        ...
    }

    def "testFeature 2" (){
        println ("--> Feature 2 runs")
        when:
        ...
        then:
        ...
    }
}
    @RunWith(Suite.class)
    @Suite.SuiteClasses([
            My_Spec.class,
            My_Spec1.class,
            My_Spec2.class
    ])
    class TestSuite extends Specification {
                @ClassRule
                public static ExternalResource testRule = new ExternalResource(){
                @Override
                        public void before() throws Throwable{
                                System.setProperty('sss', 'true')
                }
        }
}
我需要使用jUnit runner运行我的规范,因为我需要将它分组到TestSuite中。我找到了一种在testSuite运行之前设置系统属性的方法。它在JUnit4.9-@ClassRule中提供。所以,我在这里使用它。 通过这种方式,我的测试套件如下所示:

@Stepwise
class My_Spec extends GebReportingSpec {

    @IgnoreIf({properties['sss'].contains('true')})

    def "testFeature 1" (){
        println ("--> Feature 1 runs")
        given:
        println ("--> mySystemProp is: ${properties['sss']}")
        ...
        when:
        ...
        then:
        ...
    }

    def "testFeature 2" (){
        println ("--> Feature 2 runs")
        when:
        ...
        then:
        ...
    }
}
    @RunWith(Suite.class)
    @Suite.SuiteClasses([
            My_Spec.class,
            My_Spec1.class,
            My_Spec2.class
    ])
    class TestSuite extends Specification {
                @ClassRule
                public static ExternalResource testRule = new ExternalResource(){
                @Override
                        public void before() throws Throwable{
                                System.setProperty('sss', 'true')
                }
        }
}
但是@IgnoreIf behaviur不起作用:它看不到添加的系统属性“sss”,但是,在feature method中,此属性可用: 当功能运行时,它会给出下一个输出:

Running TestSuite
--> Feature 1 runs
--> mySystemProp is: true
--> Feature 2 runs
所有这些都是我使用maven安装运行的。 我的pom.xml的一部分:

        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
            <includes>
                <include>TestSuite.*</include>
            </includes>
            <systemPropertyVariables>
org.apache.maven.plugins
maven surefire插件
2.18.1
测试套件*
我做错了什么? 如果一切都是正确的-我如何才能使它与斯波克的@IgnoreIf和道具一起工作,我需要在JUnitTestSuite中定义它们? (请不要使用jUnit的@Categories。)


谢谢。

您确定
属性['sss']
从传递给
@IgnoreIf
的闭包中解析为正确的方法调用吗?在过去,我一直试图变得过于groovy,尤其是在解析系统道具时,试图使用这种带有静态导入的简洁表达式。您是否尝试将其更改为
System.getProperty(“sss”)

此外,传递给
@IfIgnore
的闭包将委托设置为
org.spockframework.runtime.extension.builtin.PremissionContext
,该委托具有
sys
属性,因此您可以改为尝试
sys[“sss”]
。如果这没有帮助,那么您可以通过将代码更改为以下内容来调试闭包中可用的属性:

@IgnoreIf({ println sys; sys["sss"].contains("true") })

你好感谢回复。1。关于“太groovy”风格:-我尝试了
System.getProperty(“sss”)
System.sss
System.properties.get(“sss”)
-但仍然没有成功。看来这不是问题。2.关于系统属性调试-有很多系统属性正在打印出来-除了“sss”。问题是:在@ClassRule,ExternalResource块中创建的系统属性不是全局的。我对JVM的内部进程行为了解不够,无法理解它为什么会这样工作((您是否尝试过使用类规则而不是使用类规则?或者可能使用
@rule
而不是
@TestRule
?此外,我不确定您在运行spock测试时是否应该使用自定义测试运行程序,因为有一个特定于spock的测试运行程序。您是否考虑过使用来实现您所追求的目标?请您解释一下您的确切用法是什么ase是,这就是为什么您希望根据应用于测试套件的规则中设置的系统属性忽略某些规范?看:我有一个注册过程测试规范。它有一些功能。有些功能是注册过程操作,但有些功能是检查(验证、接受)。我需要为已经注册的用户编写规范。因此,在此类规范的先决条件中,我需要注册一个新用户。因此,我可以使用以前的规范进行此操作。但我不需要进行所有检查!因此,如果我可以使用
@IgnoreIf
,我将能够排除所有检查,并用
@IgnoreIf
标记它们。Mybe您可以建议我其他解决方案?