'How to run test multiple times with different system property via command line in Gradle?

What is the recommended solution to run a test multiple times with different system properties via command line?

What I would like to do could look like this:

gradle clean test --tests -Dmyproperty=foo my.fancy.test.TestClass --tests -Dmyproperty=bar my.fancy.test.TestClass

I need this parameter for the setup code in the @BeforeAll method.



Solution 1:[1]

As you use junit you can use @ParameterizedTest which allows you to repeat the same junit test with different parameters. You would need to move the code for the setup from the "BeforeAll" somewhere else.

static Stream<String> testArgs() { return Stream.of(""," ", null,"12345"); }

@ParameterizedTest
@MethodSource("testArgs")
public void testByArgs(String arg){
   // Your test with asserts based on args here ...
}

Solution 2:[2]

You need to also wire those parameters up with tests' JVM through Gradle's test dsl:

// build.gradle
test {
    systemProperty 'myproperty', System.getProperty('myproperty')
}

// in your tests use it like:
@BeforeAll
fun setUp() {
  System.getProperty("myproperty")
}

Basicailly this answer.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pwnstar
Solution 2 romtsn