Pure Danger Tech


navigation
home

Assertion usability

22 Feb 2008

I’m using JUnit 3.8.2 and I’m noticing some new assertion reporting on string comparison assertion failures. It appears the string with differences is displayed in each string inside brackets.

So an assertion like: [source:java]

assertEquals(“12:01:34.000”, “12:02:56.000”);

[/source]

says something like:

[source:java]

junit.framework.ComparisonFailure: expected:<12:0[1:34].000> but was:<12:0[2:56].000>

at junit.framework.Assert.assertEquals(Assert.java:81)

at junit.framework.Assert.assertEquals(Assert.java:87)

[/source]

I get the idea here and in some cases this is a really helpful annotation. But so far, I’ve had just as many cases where it made the error harder to understand than easier. Like for instance, when the string in question had it’s own [] characters. Or if the string is entirely different, you’ll see [ ] around both the entire expected and actual string.

For me, I was easily confused enough (admittedly, I’m dim) that I spent several minutes rifling through code trying to figure out how I was inserting [ ] in the string.

Personally, I’d prefer something a little that clearly stated the real expected and actual strings (which I am really likely to want to copy/paste to either a test or an error report or code without [ ]) and separately a statement of differences. You then have to deal with the range of different kinds of differences – missing text, extra text, changed text and possibly even multiple ranges. And of course you have to take into account “small” text vs “big” text. Small text can work well in a short message, but big text probably wants only a partial display.

The Eclipse GUI will show you a nice pictorial view of this if you double-click the assertion error in the Failure Trace view. It’s not perfect, but does a good job of showing you this information.

I admit this is a problem with many constraints. But I think with the right attention to detail, you could get really stellar string matching assertion errors. Anyone want to take it on?