[ACCEPTED]-Can I make JUnit more verbose?-junit
If you want to see some output for each 15 successful assertion, another simple approach 14 which requires no external dependencies 13 or source code, would be to define your 12 own Assert class which delegates all methods 11 to the standard JUnit Assert class, as well 10 as logging successful assertions (failed 9 assertions will be reported as usual by 8 the JUnit class).
You then run a global search-and-replace 7 on your test classes from "org.junit.Assert" => "com.myco.test.Assert", which 6 should fix-up all regular and static import 5 statements.
You could also then easily migrate 4 your approach to the quieter-is-better-camp 3 and change the wrapper class to just report 2 the total # of passed assertions per test 1 or per class, etc.
Adding some info that would have been helpful 14 to me when I wanted JUnit to be more verbose 13 and stumbled on this question. Maybe it 12 will help other testers in the future.
If 11 you are running JUnit from Ant, and want 10 to see what tests are being run, you can 9 add the following to your task:
<junit showoutput="true" printsummary="on" enabletestlistenerevents="true" fork="@{fork}" forkmode="once" haltonfailure="no" timeout="1800000">
Note that 8 showoutput, printsummary, and enabletestlistenerevents 7 are what helped, not the other task attributes. If 6 you set these, you'll get output like:
Running com.foo.bar.MyTest
junit.framework.TestListener: tests to run: 2
junit.framework.TestListener: startTest(myTestOne)
junit.framework.TestListener: endTest(myTestOne)
junit.framework.TestListener: startTest(myTestTwo)
junit.framework.TestListener: endTest(myTestTwo)
Tests run: 2, Failures: 0, Errors: 0, Time elapsed: 0.495 sec
This 5 was useful to me when my tests were timing 4 out and I wasn't sure which tests were actually 3 taking too long, and which tests got cancelled 2 because they were unlucky enough to be running 1 when the time was up.
You can use AOP (with Spring or AspectJ) define 7 pointcuts on all assert methods in junit.framework.Assert 6 class. Using spring you can implement your 5 own class as after returning advice (http://static.springframework.org/spring/docs/2.5.x/reference/aop.html#aop-advice-after-returning) which 4 will only be called if the assert method 3 passed (otherwise it throws an exception: junit.framework.AssertionFailedError ). In 2 you own class you can implement a simple 1 counter and print it at the end.
Are you really interested in an assertion 10 that succeeds? Normally, the only interesting 9 assertions are ones that fail.
Being a fervent 8 JUnit devotee myself, I try and make the 7 output of the tests as quiet as possible 6 because it improves the signal-to-noise 5 ratio when something doesn't pass. The 4 best test run is one where everything passes 3 and there's not a peep from stdout.
You could 2 always work on your unit test until it succeeds 1 and run "grep Assert test.java | wc -l". :-)
Hard to be done. All assert methods are 9 static members of the class Assert, which 8 implies that the RunNotifier (which counts 7 the successful and failed tests) is not 6 within reach.
If you dont refrain from an 5 ungly hack: take the sources from JUnit, patch 4 them to store the current notifier in a 3 static field of Assert when running tests 2 such that the static methods can report 1 successful asserts to this notifier.
I'm pretty sure you can create a custom 11 TestRunner that does that. We ended up with 10 something similar in our homemade Unit-testing 9 framework (a clone of NUnit).
Oh, wait - now 8 that I'm reading your question again, if 7 you really want output for each successful 6 assertion, you'll have to dig into the plumbing 5 more. The TestRunner only gets called once 4 for each testcase start/end, so it'll count 3 passed and failed tests, not assertions.
This 2 isn't much of a problem for me, since I 1 tend towards one assertion per test, generally.
I don't think, it's the goal of JUnit to 6 count matched assertions or print out more 5 verbose information. If tests are atomic, you'll 4 get most information in there. So I would 3 review my tests.
You're also able to establish 2 a LogFile in JUnit. It's possible, but it 1 will decrease test execution performance...
This is not a straight answer to the question 7 and is in fact a misuse of a junit feature, but 6 if you need to debug some values that are 5 used in the asserts, then you can add temporarily something 4 like:
Assume.assumeTrue(interestingData, false);
This won't fail the build, but will 3 mark the test as IGNORED, and will force the 2 values to be included in the test report 1 output.
❗️ Make sure to remove the statements once you are done. Or, as an alternative, you can change the statement to
Assume.assumeTrue(interestingData, true)
in case you might want to debug it again in the future.
This question was asked a long ago but just 13 to add:
Isn't it better to get the reports 12 generated as html under the build
folder and 11 refresh the browser after every test?
Using 10 Gradle (as it offers support for Junit out 9 of the box with its plugins for integration) I'm 8 able to open the file at build/reports/tests/test/index.html
in my project 7 and check the results in a per package, per 6 class, and per method basis.
PS: You can 5 install an extension in the browser for 4 refreshing the page if it becomes annoying.
Hope 3 this helps someone if the constraints apply.
Here, here and 2 here you can find more on how to generate reports 1 for Junit test results.
junit's javadoc unfortunately says that 2 only failed assertions are recorded (http://junit.sourceforge.net/javadoc_40/index.html)
so 1 it seems it would not be possible
Can you consider ?
1) download junit source
2) to 2 modify the class org.junit.Assert to do 1 whatever modifications you're looking for
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.