-1

Is it possible to format the JUnit output on the console in order to have a customed output? For example the standard JUnit output from my actual tests is:

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    

I would like to better highlight each JUnit class. At least by adding some new lines in order to generate a similar output:

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
Sabbane
  • 2,938
  • 4
  • 21
  • 27

2 Answers2

2

I am not aware of a way to format the console output of JUnit. However you can have JUnit produce reports in various formats, for example HTML, or XML.

Also, there is a whole lot of tools out there to process the results and present them to you in a useful manner. The Eclipse IDE for Java Developers, for example, comes packed with functions to run or debug unit tests and view the results in a tree view.

You can also use a Continous Integration System such as Jenkins to run your tests. It produces an interactive HTML report of your test results if you configure it to do so.

Community
  • 1
  • 1
chiccodoro
  • 14,407
  • 19
  • 87
  • 130
  • You're right, it will be better to process the results. As a Jenkins server is used, I will look for a documentation on how to configure it. – Sabbane Feb 04 '15 at 12:18
0

If you want full control, you can implement your own test runner quite easily.

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

I did this once to write what I then considered a fancy console log in just a few pages of code. It is not hard but given that you mostly want to insert a few additional line breaks, it might be a bit overkill.

5gon12eder
  • 24,280
  • 5
  • 45
  • 92