Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.

Channels ▼

Creating and Managing Unit Tests

Adam can be contacted at [email protected].

xUnit Test Patterns: Refactoring Test Code
Gerard Meszaros
Addison-Wesley, 2007
883 pps, $54.99
ISBN-10: 0-13-149505-4

From a quality assurance and testing perspective, the greatest advance in how software is build in the last decade is the widespread use of unit testing frameworks. An application with a rich set of unit tests raises the bar of initial quality when it is given to testing groups, thereby letting them spend more time finding the difficult bugs or those which cannot be automated. Unfortunately, with this rise in test code has been a tandem rise in the complexity and fragility of the test code base, which in some organizations can of upwards of 70 percent or more of production lines-of-code (LOC). Gerard Meszaros' xUnit Test Patterns is about how to effectively create and manage your unit tests.

Meszaro divides the book into two parts:

  • The first is titled "The Narratives" and makes up the first quarter of the book. Here he discusses test automation principles, strategies and organization of test code. This section should be considered mandatory reading for all developers who are creating unit tests. It should also be recommended reading for people working in QA/Test roles as they need to be the champions of unit testing and advocate for constantly improving tests.

  • The other three quarters of the book is more developer-centric and deals with Patterns. The Patterns section coverage is further subdivided into three sections: smells (symptoms of a problem), test patterns (a starting point for writing good, robust automated tests), and test organization patterns (to help make test code easy to understand and find). Each pattern contains a description, some variations of the pattern you might encounter, and an example of the pattern in Java. The Patterns section is not meant to be read front-to-back. Instead, the backcover lists all the patterns in the context of which problems they solve. When you encounter a problem (Which fixture strategy should we use? How do we implement Behavior Verification?) with your unit tests you can quickly look up the pattern (or patterns) which address it and can concentrate on those that are relevant to your current needs.

At one point I worked as a test lead at a company which made use of the Stub Test Double in its application to remove the dependency of partner systems being available which saved out release dates on more than one occasion. We also used started writing automate GUI tests against the stubbed version with much success. In talking with other organizations about the use of Test Doubles, I often hear things like "We tried that for a while, but the scripts kept breaking" or "We are not sure where we should use a Mock." It was those sorts of questions which lead me to xUnit Test Patterns and it's coverage of Object Doubles (Stubs, Spies, Mocks and Fakes). These patterns are the most important part of the book.

I highly recommend this book and see it becoming required reading for both test and development teams. Automated unit tests are an essential part of today's mature development environment. This book will help teams make their existing tests more robust, more extensible and of higher quality. If you are not yet writing test code, xUnit Test Patterns will help you do it right the first time.

Related Reading

More Insights

Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.