Change the future

Saturday 11:30 a.m.–noon

Things to make writing tests easier

Chris Withers

Audience level:


Writing tests is often hard enough as it is, without having to keep on writing the same boiler plate to set things up and check outcomes. I've collected a bunch of these tools that are all documented, flexible and have unit tests of their own. I'd like to share those with people interested in using them so they can spend more time writing code and less time worrying about how to test it properly.


In this talk I'll go through the tools I use most often when writing unit tests; Michael Foord's Mock and my own TestFixtures package.

Using these tools I'll walk through how I tackle the following common testing challenges:

Comparing objects and sequences

Showing how to get better feedback when the results aren't as you expected along with how to compare objects that don't normally support comparison.

Mocking out objects and methods

Covering easy to use ways of stubbing out objects, classes or individual methods. The special subtleties of testing with dates of 'today' and times of 'now' are covered in some detail.

Testing logging

Often overlooked, checking that your code logs the messages you expect can be made easy with TestFixtures' specially tailored capturing log handler.

Testing stream output

It's often normal for code to print to the console and it can be just as easy to capture stream output and making assertions about it.

Testing with files and directories

Working with files and directories in tests can be hard when done from first principles. With some handy utilities it can be made easy.

Testing exceptions

Showing not only how to assert that the correct type of exception is raised, but also how to make sure the message in the exception is correct and useful to those who might see it.