是否可以在控制台上格式化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类。至少通过添加一些新行来生成类似的输出:
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
答案 0 :(得分:2)
我不知道如何格式化JUnit的控制台输出。但是,您可以让JUnit以各种格式生成报告,例如HTML或XML。
此外,还有很多工具可以处理结果并以有用的方式呈现给您。例如,Eclipse IDE for Java Developers包含运行或调试单元测试的函数,并在树视图中查看结果。
您还可以使用Jenkins等连续集成系统来运行测试。如果您将其配置为执行此操作,它会生成测试结果的交互式HTML报告。
答案 1 :(得分:0)
如果您想要完全控制,您可以非常轻松地实现自己的测试运行器。
从RunListener
派生 - 一个处理testStarted
,testFinished
,testFailure
等的类,然后将其实例作为addListener()
传递给JUnitCore
我这样做了一次,只用几页代码就可以编写我认为是花哨的控制台日志。这并不难,但鉴于你大多想要插入一些额外的换行符,可能有点矫枉过正。