The state of my testing art

This post is prompted by a recent (or fairly old by modern speed standards) entry by Jay Fields. My own experience over the past 3 years has been exactly what he describes, dead ends, changes, improvements which leave the test code base in a state fit for archeological research.

The biggest pitfalls I fell into (and made other people fall into, but they did not have anything better to offer): too many assertions per test, too many mock expectations, too much set up, too much coupling with the implementation. A lot of those problems stem from the overall quality of our design at the object level which in turn is explained partly by our immaturity in using tests to shape the low level interactions between objects.

I think we are doing a bit better now and I want to share what I try to do now.

Here is an example for a unit test:

@Test
public void putShouldStoreValue() {
        givenAPropertyName();
        givenAPropertyValue();

        BaseMutableDataStore testedContainer = new BaseMutableDataStore();
        testedContainer.put(propertyName(), propertyValue());

        assertSame(propertyValue(),testedContainer.get(propertyName()));
}

This uses primarily the builder idea that Jay presented to remove the technical details of stubbing and set-ups. The idea, taken from BDD frameworks and rspec/cucumber in particular is to focus the text of the test on the assumptions and the assertions. Nothing here is new. But I had I read an example like this 3 years ago, I would have saved myself and my team-mates a lot of trouble.

I am currently trying to expand the same presentation to integration tests that our QA could review and help improve and refer to as part of their testing plans. Hopefully more on this in a future post.

Also, as I am keen on continuously improving, please do not hesitate to help me get better (my special thanks to Jay for his posts already).

Post to Twitter

One Response to “The state of my testing art”

  1. One brike at a time » Blog Archive » Adding ‘given a’ to rspec Says:

    [...] the past few months I have grown to like the ‘given a’ syntax more and more (described here). So when I recently reactivated my Ruby On rails home project (see brikeyard deployed on heroku), [...]

Leave a Reply