Home
subscribe

L'etat, c'est moi

Mere Complexities sells the consulting and development services of me, Paul Wilson.

Conferences

Organising Scotland on Rails
Speaker, RailsConf Europe '08

Archive

Minimal assertions

Assertion messages are useful when you need to locate just which assertion failed in a multiple assertion xUnit test. For readability and maintainability these should be short; I prefer not to repeat information that will be added by the test framework’s own failure message. Instead of

assertEquals(“There should be 23 widgets”, 23, testee.getWidgetCount());

I would write
assertEquals(“Widget count”, 23, testee.getWidgetCount());

On failure Junit tells you all you need to know:junit.framework.AssertionFailedError: Widget count expected:<23> but was:<22>.

When testing boolean values I favour using assertEquals as it is more expressive than assertTrue, reducing the information needed in the assertion message. Instead of writing

assertTrue(“Should have started”, testee.isStarted());
assertFalse(“Should not have finished”, testee.isFinished());

I prefer
assertEquals(“Started”, true, testee.isStarted());
assertEquals(“Finished”, false, testee.isFinished());

All