Rob Oxspring
Rob Oxspring

Reputation: 2935

Detecting JUnit "tests" that never assert anything

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

Answers (3)

Joe
Joe

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

ptyx
ptyx

Reputation: 4164

Since that's a one time operation I would:

  • scan all test methods (easy, get the jUnit report XML)
  • use an IDE or other to search references to Assert.*, export result as a list of method
  • awk/perl/excel the results to find mismatches

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

John B
John B

Reputation: 32949

Not sure of a tool, but the thought that comes to mind is two-fold.

  1. 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).

  2. 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

Related Questions