Using command line arguments or system properties from a Parameterized Junit test?

自作多情 提交于 2019-12-12 11:47:44

问题


I use this method to setup my parameterized data:

@Parameterized.Parameters
public static Collection<Object[]> getStories() {
        Collection<Object[]> allStories = new ArrayList<Object[]>()
        new File(SPEC_DIRECTORY).eachFileRecurse(FileType.FILES) { file ->
            if (file.getName().endsWith('.story')) {
                Object[] args = [file.getName(), file]
                allStories << args
            }
        }
        return allStories
    }

I invoke the main test with gradle via gradle test, but I do not seem to be able to see system properties.

If I use the invocation gradle -Dfile=test.story, then System.getProperty('file') is undefined. How can I pass arguments to this parameterized data builder?


回答1:


Gradle runs all tests in a separate JVM. You have to configure the test task:

test {
    systemProperty "file", "test.story"
}

For more information, see the Gradle DSL reference.




回答2:


In the interest of any beginners out there(like myself) who stumble upon this question; I'd like to explicitly mention that the systemProperty in gradle test task simply passes on system properties to the test jvm. So if you simply use

test{
systemProperty 'Chrome', 'Browser' }   

You won't be able to access the property in your junit tests via ' System.getProperty("Browser") ' . Instead it needs to be coupled with the command gradle test -DBrowser=Chrome

The above case is merely an illustration and it would probably make sense to use -

test{
    systemProperty 'Chrome', System.getProperty('Browser') } 

along with

gradle test -DBrowser=Chrome


来源:https://stackoverflow.com/questions/8855296/using-command-line-arguments-or-system-properties-from-a-parameterized-junit-tes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!