Making Good Software

A blog by Alberto G (Alberto Gutierrez)

Written by Alberto Gutierrez

August 16th, 2009 at 12:26 pm

Book review: xUnit Test Patterns

with one comment

I’ve just read xUnit Test Patterns – Refactoring Test Code by Gerard Meszaros, this is a book from the “Martin Fowler signature series”, meaning that he has reviewed and advised on the contents of the books, as a result, you will find that the structure of this book is pretty similar to Refactoring: Improving the design of existing code.


These are the main points I’ve taken from the book.

  1. Unifies terminology and provides with some vocabulary to help promoting open discussions about testing. Some of the common terminology provided in the book is:
    • SUT: System under test, the component you are testing.
    • DOC: Depended-on component, component which depends from the SUT, like a database or a third party library.
    • Test Double: A component that substitutes a DOC to help us testing the SUT isolated and that will make the testing easier.
    • Fixture: The set of data that needs to be prepared for each test.
  2. Highlights the fact that every test consists on 4 steps.
    • Setup: Step where the fixture for the test is prepared.
    • Exercise: The fixture is passed to the SUT and the functionality to be tested is executed.
    • Verification: Ensures that in the previous stage, Exercise, everything happened as expected.
    • Tear Down: The fixture is removed.
  3. Introduces test smells: Test smells are indicators that your tests are not clean and that you may have maintenance problems with them in the future, you are encouraged to refactor them. Some of the most important test smells are.
    • Buggy tests: Tests where is usual to find bugs.
    • Frequent debugging. If you have a good set of test, debugging shouldn’t be necessary, if you find yourself debugging your code, you may want to write more tests or refactor the ones you have.
    • Test code duplication. Same as DRY, don’t duplicate code in your tests either…
  4. Encourages to have clean test code.
  5. Sets a few goals and principles for the tests, some of them are:
    • Expressive tests. Test should be very easy to read and should be written in such a way that they can be used as documentation on how to use the system
    • Fast tests. Tests should always be fast to execute.
    • Independent tests. The result of a particular test should not depend in the order of its execution.
  6. Introduces some patterns to deal with common problems when writing tests. Some of them are:
    • Test with doubles. How to make sure that the SUT behaves as expected when it has dependencies with other components as databases or complex elements. The four types of doubles are Mock, Stub, Dummy and Fake.
    • Test organization patterns. Patterns to help you decide how to structure your tests.
    • Fixture setup patterns. Patterns to help you create your fixtures.

What I liked from this book

This is a great book, as far as I know is the deepest book on how to write good tests, it is not too technical, which is really good as it can be useful no matter what your programming language is. It is very well structured and I’m sure will be used in the future as reference to discuss subjects related with testing.

What I didn’t like from this book

Even thought, as I said, I think this a great book, there are a couple of things I would have changed.

  1. The title. They call the book xUnit Test Patterns, and they actually use JUnit for most of the examples in the book, but the scope of the book is much bigger.
  2. The length. Is too long, I believe they could have fit the same concepts and ideas in maybe half of the pages they use.


If you want to improve your unit tests, no matter if you use xUnit or TestNG or any other framework, buy it, it’s a great book, what I have explained of this review is actually just an small portion of all the patterns, goals, and test smells that the book explain.