Reputation: 2935
We used to have a technical director who liked to contribute code and was also very enthusiastic about adding unit tests. Unfortunately his preferred style of test was to produce some output to screen and visually check the result.
Given that we have a large bank of tests, are there any tools or techniques I could use to identify the tests never assert?
Upvotes: 5
Views: 193
Reputation: 31087
If those tests are the only ones that produce output, an automated bulk replacement of System.out.println(
with org.junit.Assert.fail("Fix test: " +
would highlight exactly those tests that aren't pulling their weight. This technique would make it easy to inspect those tests in an IDE after a run and decide whether to fix or delete them; it also gives a clear indication of progress.
Upvotes: 1
Reputation: 4164
Since that's a one time operation I would:
Edit: another option is to just look for references to System.out or whatever his preferred way to output stuff was, most tests won't have that.
Upvotes: 2
Reputation: 32949
Not sure of a tool, but the thought that comes to mind is two-fold.
Create a TestRule class that keeps track of the number of asserts per test (use static counter, clear counter at beginning of test, assert that it is not 0 at end of test).
Wrap the Assert class in your own proxy that increments the TestRule's counter each time it is called.
Is your Assert class is called Assert that you would only need to update the imports and add the Rule to the tests. The above described mechanism is not thread-safe so if you have multiple tests running concurrently you will be incorrect results.
Upvotes: 1