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

I use this method to configure 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 am invoking the main test with gradle via gradle test , but it looks like I don't see the system properties.

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

+4
source share
2 answers

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

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

See the Gradle DSL link for more information.

+5
source

In the interest of all newcomers (such as myself) who stumble upon this question; I would like to explicitly point out that the systemProperty test task in gradle just passes the system properties to the test jvm. Therefore, if you just use

 test{ systemProperty 'Chrome', 'Browser' } 

You cannot access the property in junit tests through "System.getProperty (" Browser ")." Instead, it should be associated with the gradle test -DBrowser = Chrome command

The above case is just an illustration, and it probably makes sense to use -

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

together with

 gradle test -DBrowser=Chrome 
0
source

Source: https://habr.com/ru/post/893900/


All Articles