Serban
Serban

Reputation: 812

JUnit5 - can I parameterize execution of entire test class?

Starting from the following generic code, showing a sample test class that needs to be executed for several rounds of input data:

import java.util.stream.Stream;
import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class DummyManyTests {

    @BeforeAll
    public void beforeAll() {
        log.info("before all...");
    }

    @AfterAll
    public void afterAll() {
        log.info("after all...");
    }

    static Stream<Integer> countParameterProvider() {
        return Stream.of(50, 100, 200);
    }

    @ParameterizedTest
    @MethodSource("countParameterProvider")
    @Order(10)
    public void m1(int count) {
        log.info("m1 #count: {}", count);
    }

    @ParameterizedTest
    @MethodSource("countParameterProvider")
    @Order(20)
    public void m2(int count) {
        log.info("m2 #count: {}", count);
    }

    @ParameterizedTest
    @MethodSource("countParameterProvider")
    @Order(30)
    public void m3(int count) {
        log.info("m3 #count: {}", count);
    }

}

Which generates this output:

15:38:54.878 [main] INFO com...DummyManyTests - before all...
15:38:54.973 [main] INFO com...DummyManyTests - m1 #count: 50
15:38:55.004 [main] INFO com...DummyManyTests - m1 #count: 100
15:38:55.007 [main] INFO com...DummyManyTests - m1 #count: 200
15:38:55.013 [main] INFO com...DummyManyTests - m2 #count: 50
15:38:55.015 [main] INFO com...DummyManyTests - m2 #count: 100
15:38:55.018 [main] INFO com...DummyManyTests - m2 #count: 200
15:38:55.021 [main] INFO com...DummyManyTests - m3 #count: 50
15:38:55.025 [main] INFO com...DummyManyTests - m3 #count: 100
15:38:55.028 [main] INFO com...DummyManyTests - m3 #count: 200
15:38:55.030 [main] INFO com...DummyManyTests - after all...

I need to change the way the tests are parameterized.
Basically I need to have the entire entire class executed end-to-end (including beforeAll and afterAll), for each value of that parameter.

The output would then become:

15:38:54.878 [main] INFO com...DummyManyTests - before all...
15:38:54.973 [main] INFO com...DummyManyTests - m1 #count: 50
15:38:55.013 [main] INFO com...DummyManyTests - m2 #count: 50
15:38:55.021 [main] INFO com...DummyManyTests - m3 #count: 50
15:38:55.030 [main] INFO com...DummyManyTests - after all...
15:38:54.878 [main] INFO com...DummyManyTests - before all...
15:38:55.004 [main] INFO com...DummyManyTests - m1 #count: 100
15:38:55.015 [main] INFO com...DummyManyTests - m2 #count: 100
15:38:55.025 [main] INFO com...DummyManyTests - m3 #count: 100
15:38:55.030 [main] INFO com...DummyManyTests - after all...
15:38:54.878 [main] INFO com...DummyManyTests - before all...
15:38:55.007 [main] INFO com...DummyManyTests - m1 #count: 200
15:38:55.018 [main] INFO com...DummyManyTests - m2 #count: 200
15:38:55.028 [main] INFO com...DummyManyTests - m3 #count: 200
15:38:55.030 [main] INFO com...DummyManyTests - after all...

How can I achieve this ?

Upvotes: 0

Views: 299

Answers (0)

Related Questions