如何格式化 JUnit 控制台输出?

How to format JUnit console output?

是否可以在控制台上格式化 JUnit 输出以获得自定义输出? 例如,我实际测试的标准 JUnit 输出是:

junits:
[junit] Running com.myapp.tests.dao.Test_1
[junit] Testsuite: com.myapp.tests.dao.Test_1
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 10,016 sec
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 10,016 sec
[junit]
[junit] Testcase: insert took 3,984 sec
[junit] Running com.myapp.tests.dao.Test_2
[junit] Testsuite: com.myapp.tests.dao.Test_2
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 13,35 sec
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 13,35 sec
[junit]
[junit] Testcase: load took 3,239 sec
[junit] Testcase: insert took 2,21 sec
[junit] Running com.myapp.tests.dao.Test_3
[junit] Testsuite: com.myapp.tests.dao.Test_3
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 12,786 sec
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 12,786 sec
[junit]
[junit] Testcase: insert took 2,607 sec
[junit] Testcase: update took 2,338 sec
[junit] Running com.myapp.tests.dao.Test_4
[junit] Testsuite: com.myapp.tests.dao.Test_4
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 14,803 sec
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 14,803 sec
[junit]
[junit] Testcase: getAll took 4,73 sec
[junit] Testcase: insert took 2,289 sec    

我想更好地突出每个 JUnit class。至少通过添加一些新行以生成类似的输出:

junits:
[junit] Running com.myapp.tests.dao.Test_1
[junit] Testsuite: com.myapp.tests.dao.Test_1
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 10,016 sec
[junit] Tests run: 1, Failures: 0, Errors: 0, Time elapsed: 10,016 sec
[junit]
[junit] Testcase: insert took 3,984 sec
[junit]
[junit]
[junit]
[junit] Running com.myapp.tests.dao.Test_2
[junit] Testsuite: com.myapp.tests.dao.Test_2
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 13,35 sec
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 13,35 sec
[junit]
[junit] Testcase: load took 3,239 sec
[junit] Testcase: insert took 2,21 sec
[junit]
[junit]
[junit]
[junit] Running com.myapp.tests.dao.Test_3
[junit] Testsuite: com.myapp.tests.dao.Test_3
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 12,786 sec
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 12,786 sec
[junit]
[junit] Testcase: insert took 2,607 sec
[junit] Testcase: update took 2,338 sec
[junit]
[junit]
[junit]
[junit] Running com.myapp.tests.dao.Test_4
[junit] Testsuite: com.myapp.tests.dao.Test_4
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 14,803 sec
[junit] Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 14,803 sec
[junit]
[junit] Testcase: getAll took 4,73 sec
[junit] Testcase: insert took 2,289 sec

我不知道如何格式化 JUnit 的控制台输出。但是,您可以让 JUnit 生成各种格式的报告,例如 HTML, or XML.

此外,还有很多工具可以处理结果并以有用的方式呈现给您。例如,Eclipse IDE for Java Developers 包含用于 运行 或调试单元测试并在树视图中查看结果的函数。

您还可以使用 Jenkins 等持续集成系统来 运行 您的测试。如果您将其配置为这样做,它会生成测试结果的交互式 HTML 报告。

如果你想要完全控制,你可以很容易地实现你自己的测试运行器。

来自 RunListener – a class that handles testStarted, testFinished, testFailure etc – and then pass an instance of it as via addListener() to the JUnitCore.

我曾这样做过一次,只用了几页代码就编写了我当时认为是精美的​​控制台日志。这并不难,但考虑到您主要想插入一些额外的换行符,这可能有点矫枉过正。