java 从 Junit 运行时 System.out.print 不会输出到控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37372200/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 02:26:11  来源:igfitidea点击:

System.out.print doesn't output to console when running from Junit

javamultithreadingjunitjunit4system.out

提问by Lika

When running:

运行时:

public static void main(String... args) throws InterruptedException {
    while (true) {
        System.out.print(".");
        Thread.sleep(200);
    }
}

vs. when running same code from junit:

与从 junit 运行相同的代码时:

@Test
public void test() throws Exception {
    while (true) {
        System.out.print(".");
        Thread.sleep(200);
    }
}

There is different behaviour:
for the main() - the output is displayed as expected, as the process runs ("." -> ".." -> "...")

有不同的行为:
对于 main() - 当进程运行时,输出按预期显示(“.” -> “..” -> “...”)

however, for JUnit, when running same piece of code, No output is displayed as the process runs - It's flushed only when quitting the test.

但是,对于 JUnit,当运行同一段代码时,进程运行时不会显示任何输出 - 只有在退出测试时才会刷新它。

Why does this happen? Is there any way to workaround it if I need the status to be shown in console during the test run?

为什么会发生这种情况?如果我需要在测试运行期间在控制台中显示状态,有什么办法可以解决它?

I need to print to the same line, so using println wouldn't suit.

我需要打印到同一行,所以使用 println 不适合。

采纳答案by Lika

There are many ways people run JUnit tests (from within an IDE, from within a build system like Maven, or from a command line that uses the JUnit library directly). It appears that the way that you're running it uses a standard output that doesn't flush on every output. (This is probably intentional, as often tests are run in a batch using a continuous integration system, and the logs reviewed afterward, so not flushing on every write can improve performance.)

人们可以通过多种方式运行 JUnit 测试(从 IDE 内部、从构建系统(如 Maven)或从直接使用 JUnit 库的命令行)。看来您运行它的方式使用的标准输出不会在每个输出上刷新。(这可能是有意为之,因为通常使用持续集成系统批量运行测试,然后查看日志,因此不要在每次写入时刷新可以提高性能。)

But, if you need to flush the buffer explicitly, try using System.out.flush();after each .printcall.

但是,如果您需要显式刷新缓冲区,请System.out.flush();在每次.print调用后尝试使用。

Another option, depending on what you're actually looking to do, may be to use a more full-featured logging system than the built-in System.out stream.

另一种选择,取决于您实际想要做什么,可能是使用比内置 System.out 流更全功能的日志系统。

回答by Leandro Tavares

Ctrl + Shift + p and type show test output. Alternatively you can open the output window (Ctrl + J) and choose to see Test Output from a combo box on it's right upper corner.

Ctrl + Shift + p 并键入 show test output。或者,您可以打开输出窗口 (Ctrl + J),然后从右上角的组合框中选择查看测试输出。

回答by Drakes

This still doesn't work in IntelliJ 2019. *sigh*

这在 IntelliJ 2019 中仍然不起作用。 *叹气*

@Test
public void test() throws Exception {
    while (true) {
        System.out.print(".");
        Thread.sleep(200);
    }
}

To achieve somewhat what you are looking for I had to force a newline periodically like this:

为了实现您正在寻找的东西,我不得不像这样定期强制换行:

@Before
public void forceConsoleOutput()
{
    new Thread( () -> {
        for ( ; ; )
        {
            // Does nothing. Why JetBrains?
            // System.out.flush();

            System.out.println();
            try
            {
                Thread.sleep( 5000 );
            }
            catch ( InterruptedException e )
            {
                e.printStackTrace();
            }
        }
    } )
    {{
        start();
        System.out.println( "Terminated" ); // The threads naturally stop between method tests
    }};
}