Lik*_*ika 9 java junit multithreading junit4 system.out
运行时:
public static void main(String... args) throws InterruptedException {
while (true) {
System.out.print(".");
Thread.sleep(200);
}
}
Run Code Online (Sandbox Code Playgroud)
从junit运行相同的代码时:
@Test
public void test() throws Exception {
while (true) {
System.out.print(".");
Thread.sleep(200);
}
}
Run Code Online (Sandbox Code Playgroud)
有不同的行为:
对于main() - 输出按预期显示,因为进程运行("." - >".." - >"...")
但是,对于JUnit,当运行相同的代码时,进程运行时不显示输出 - 仅在退出测试时刷新.
为什么会这样?如果我需要在测试运行期间在控制台中显示状态,有没有办法解决它?
我需要打印到同一行,所以使用println不适合.
小智 6
人们可以通过多种方式运行JUnit测试(从IDE中,从Maven之类的构建系统中,或者从直接使用JUnit库的命令行中)。看来,它的运行方式使用的标准输出不会在每个输出上都刷新。(这可能是有意的,因为通常使用连续集成系统以批处理方式运行测试,然后再检查日志,因此不对每次写操作进行刷新都可以提高性能。)
但是,如果需要显式刷新缓冲区,请System.out.flush();
在每次.print
调用后尝试使用。
根据您实际要执行的操作,另一种选择可能是使用比内置System.out流更全功能的日志记录系统。
归档时间: |
|
查看次数: |
13672 次 |
最近记录: |