Junit并行测试日志相互混淆

时间:2019-09-01 15:09:09

标签: junit log4j2 parallel-testing

我有一些并行运行的测试类和方法。 我正在使用log4j2进行日志记录,但是控制台的日志输出相互混淆。

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
public class TestClass1 {
    Logger log = LogManager.getLogger();


    @Test
    public void test1() {
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.info("Performing test1 method in " + this.getClass().getName());
    }

    @Test
    public  void test2() {
        log.info("Performing test2 method in " + this.getClass().getName());
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}

期望在test1 TestClass1中看到输出-在TestClass1中执行test1方法

以及在tests2 TestClass1中-在TestClass1中执行test2方法

当前状态:两个输出在第一次或第二次测试中一起打印,或者仅打印一个测试。 enter image description here enter image description here

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

关于opened bug in JetBrains,此问题将不会在某天得到解决。 我尝试使用下面错误中提到的TestReporter注入,但是它也不起作用。

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestReporter;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
public class TestClass1 {

    @Test
    public void test1(TestReporter testReporter) {
        testReporter.publishEntry("test1 started");
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    @Test
    public void test2(TestReporter testReporter1) {
        testReporter1.publishEntry("test2 started");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

}

enter image description here