xUnit Test Patterns: Refactoring Test Code

 

Refactoring Test Code?
by Scott Brookhart

 

 On first seeing this title, I thought, “Do I actually need to spend the time to refactor test code? I just need to refactor my application code so that it passes the tests.” Then I thought again—this could be useful. On reading the book, I realized that my tests were missing some features that would, for example, make them more maintainable and robust.


I highly recommend xUnit Test Patterns to anyone who has considered test automation and unit testing on software projects or would like to know more about them. It provides detailed information on using xUnit—the name given to testing frameworks such as JUnit, NUnit, and so on. While I’ve read many books that discuss the benefits of test automation, test-driven development, and xUnit, none has addressed test code patterns or refactoring at this depth. Martin Fowler, a well-known and respected author, developer, and contributor to the software engineering world, writes the foreword to xUnit Test Patterns. He compares it to the gang of four’s Design Patterns. Just as Design Patterns unlocked the hidden pattern gems in object-oriented systems, this book unlocks the patterns for xUnit.


Author Gerard Meszaros organizes the book into three parts—The Narratives, The Test Smells, and The Patterns. In the narratives, he reviews the philosophy and principles of test automation and presents some background on unit test frameworks.For readers who might not completely agree with the agile test-driven approach, the narratives provide a comprehensive rationale for its utility.


However, the target audience is software project testers, developers, and managers. The book addresses the xUnit test patterns and efforts they need to develop an effective test automation environment. It looks at ways to write tests better and faster. It also discusses such variables as hard-to-test code, object-oriented legacy software (without tests) and non-object-oriented legacy software. Chapters are organized to help readers go directly to the pattern that will benefit them. The pattern section format tells readers how the pattern works and when to use it; it provides implementation notes, refactoring notes, and sample code as well.

Readers who have the time to go cover to cover the entire book will benefit from its thoroughness and clarity. But if you’re already familiar with the philosophy and principles, you could turn directly to the detailed patterns and skim the narratives and the test smells that indicate something is amiss and might need refactoring.

The mantra throughout the book is setup, exercise, verify, and teardown. Each of these activities is evaluated in the context of principles, refactorings, and patterns. Depending on the context, developers writing test code may need patterns or refactorings to address shortcomings in each activity. Because the author addresses the smells in the context of these four activities, I found it easy to understand how to go about refactoring test code.

Maczaros offers many methods for writing good tests that are robust, consistent, and free of dependency issues. He uses code samples and diagrams to explain the points throughout. He also covers databases in tests, which I found useful because our organization has had some difficulty with databases and automated testing. Specifically, we’ve found it hard to establish and maintain the state that the tests require. The principles and strategies in this book provided a better perspective for setting up more robust tests.

This book should be on the bookshelf of anyone that must become intimately acquainted with test automation and xUnit. It’s quite effective as a catalog of xUnit patterns and software designs for greater testability, putting a compendium of xUnit patterns at your beck and call. It might also be suitable to coursework as part of software project test curriculum.

The book was easy to read and maintained a consistent look and feel. I haven’t a found another book that brings patterns, refactoring, and test automation topics together so completely. I look forward to greater success in test automation as a result of reading this book and having more confidence in my test code.

Scott Brookhart  is a senior member of the IEEE. Contact him at sbrookhart@ieee.org.

 

Showing 69 - 69 of 72 results.
Items per Page 1
of 72