6

My question is about how to run in JUnit 5 test classes in parallel.

For example I have two simple test classes which represent two separated test pipeline and they should to run parallel. However the inner tests in class should run sequentially.

public class TestClass1 {

    @Test
    public void test1() {
        System.out.println("Executing test 1 in Class 1");
        System.out.println(Thread.currentThread().getId());
        try {
            Thread.sleep(5000);
            Assert.assertTrue(false,"Assertion error in Test 1 Class 1");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test2() {
        System.out.println("Executing test 2 in Class 1");
        Assert.assertTrue(false,"Assertion error in Test 2 Class 1");
        System.out.println(Thread.currentThread().getId());
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

public class TestClass2 {

    @Test
    public void test2() {
        System.out.println("Executing test 1 in Class 2");
        System.out.println(Thread.currentThread().getId());
        Assert.assertTrue(false,"Assertion error in Class 2");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
Sergey Emeliyanov
  • 5,158
  • 6
  • 29
  • 52
natanbig
  • 79
  • 1
  • 1
  • 5

3 Answers3

5

According to the Junit5 docs

Configuration parameters to execute top-level classes in parallel but methods in same thread

junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.mode.default = same_thread junit.jupiter.execution.parallel.mode.classes.default = concurrent

Ezequiel
  • 3,477
  • 1
  • 20
  • 28
2

Basically you need the following:
1. to specify the "Execution order" for each class:

@Execution(ExecutionMode.CONCURRENT)
public class TestClass1 { 
    ...
}
@Execution(ExecutionMode.CONCURRENT)
public class TestClass2 {
    ...
}

2. add config file with desired parallel parameters. There is configuration options: Junit5 User Guide

I have a demo project with parallel configured on GitHub

  • 1
    I also used in test method order annotation ` @TestMethodOrder(OrderAnnotation.class)` Once I added the `@Execution(ExecutionMode.CONCURRENT)`, the tests in class also running in random sequence. I also added junit-platform.properties according to @Ezequiel note above with following parameters: `junit.jupiter.execution.parallel.enabled=true junit.jupiter.execution.parallel.config.strategy=fixed junit.jupiter.execution.parallel.config.fixed.parallelism=4 junit.jupiter.execution.parallel.mode.classes.default = concurrent junit.jupiter.execution.parallel.mode.default = same_thread` – natanbig Aug 29 '19 at 10:25
  • 1
    @natanbig in this case you need: `junit.jupiter.execution.parallel.mode.default = same_thread` and `junit.jupiter.execution.parallel.mode.classes.default = concurrent` – Dragon Margarin Aug 30 '19 at 12:32
0

To preserve entire class tests sequence I added @Execution(ExecutionMode.SAME_THREAD) under each test method. The last inconvenience which I experiencing in running parallel tests is logging disorder. So once two tests were started in parallel the first one which started is only display the log output. I'm using static logger in each helper class: protected static final org.apache.logging.log4j.Logger log = LogManager.getLogger();

natanbig
  • 79
  • 1
  • 1
  • 5