0

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.

Joe
  • 287
  • 3
  • 13

2 Answers2

0

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 ...
}
Pwnstar
  • 2,333
  • 2
  • 29
  • 52
  • In case my question is unclear. What I need is the passed parameter for the setup code of the test class. I added this hint to my initial question. – Joe Feb 17 '22 at 13:14
  • 1
    There is no such thing. If you need different setups why don't you write different tests? You can always create a class which holds all the "duplicate" code for test purposes. – Pwnstar Feb 17 '22 at 13:19
  • This was my initial idea to write multiple tests. But I thought there is support from Gradle. I will follow your suggestion and write multiple classes. – Joe Feb 17 '22 at 13:27
0

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.

romtsn
  • 11,704
  • 2
  • 31
  • 49
  • If I understand your example correctly, then I need to run Gradle for each test separately. – Joe Feb 17 '22 at 15:58