Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make JUnit more verbose?

Tags:

I'd like to have it yell hooray whenever an assert statement succeeds, or at the very least have it display the number of successful assert statements that were encountered.

I'm using JUnit4.

Any suggestions?

like image 716
Allain Lalonde Avatar asked Oct 07 '08 21:10

Allain Lalonde


People also ask

How do you set the log level to debug in JUnit tests?

properties goes in the directory src/main/resources . My test version goes in src/test/resources . The Eclipse build path (classpath) is set up to search src/test/resources before src/main/resources , so your unit tests use the test file. The JAR (or WAR) build instructions use the files from src/main/resources .

Is JUnit deprecated?

framework to org. junit. Assert in JUnit 4.0 - you can use that instead, it's not deprecated.

How do you debug in JUnit?

In the case of a test failure you can follow these steps to debug it: Double click the failure entry from the Failures tab in the JUnit view to open the corresponding file in the editor. Set a breakpoint at the beginning of the test method. Select the test case and execute Debug As>JUnit Test from the Debug drop down.

How can an individual unit test method be executed or debugged?

Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T). If individual tests have no dependencies that prevent them from being run in any order, turn on parallel test execution in the settings menu of the toolbar.


2 Answers

If you want to see some output for each successful assertion, another simple approach which requires no external dependencies or source code, would be to define your own Assert class which delegates all methods to the standard JUnit Assert class, as well as logging successful assertions (failed assertions will be reported as usual by the JUnit class).

You then run a global search-and-replace on your test classes from "org.junit.Assert" => "com.myco.test.Assert", which should fix-up all regular and static import statements.

You could also then easily migrate your approach to the quieter-is-better-camp and change the wrapper class to just report the total # of passed assertions per test or per class, etc.

like image 162
Dov Wasserman Avatar answered Oct 05 '22 06:10

Dov Wasserman


Adding some info that would have been helpful to me when I wanted JUnit to be more verbose and stumbled on this question. Maybe it will help other testers in the future.

If you are running JUnit from Ant, and want to see what tests are being run, you can add the following to your task:

<junit showoutput="true" printsummary="on" enabletestlistenerevents="true" fork="@{fork}" forkmode="once" haltonfailure="no" timeout="1800000"> 

Note that showoutput, printsummary, and enabletestlistenerevents are what helped, not the other task attributes. If 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 was useful to me when my tests were timing out and I wasn't sure which tests were actually taking too long, and which tests got cancelled because they were unlucky enough to be running when the time was up.

like image 37
rzrelyea Avatar answered Oct 05 '22 06:10

rzrelyea