Preface

This book presents a comprehensive review of the xUnit family of unit test frameworks, including their usage, architecture, and theory. We begin by building a simple unit test framework from the ground up. The xUnit architecture is presented, using the JUnit framework as the reference implementation of xUnit. We progressively build an example application to demonstrate common practices and patterns of unit test development. Several popular versions of xUnit, including JUnit, CppUnit, NUnit, PyUnit, and XMLUnit, are covered in detail. Detailed class references are provided for JUnit and CppUnit as appendixes.

As a software development methodology, unit testing incorporates many rules and guidelines. However, writing unit tests is an art, not a science. Once you are familiar with the unit test driven approach to development, rigidly following its rules is optional. The true value of unit testing is in the focus on low-level software quality it gives developers, rather than as a formal process.

Audience

This book is intended for software developers, technical managers, and quality assurance staff who are learning about unit testing and agile development. Agile development is the wave of the future in software engineering, and many technical organizations are adopting it. Using unit test frameworks to enable test driven development is a key to becoming agile.

Contents of This Book

Here is a summary of the topics covered in each chapter and appendix:

Chapter 1

An overview that explains what unit test frameworks are and how they are used.

Chapter 2

A tutorial that creates a simple Java test framework. This provides the fundamentals of how unit test frameworks work. Appendix A contains the C++ version of this simple framework tutorial.

Chapter 3

A review of xUnit, using JUnit as a reference implementation to demonstrate basic xUnit architecture and usage.

Chapter 4

An overview of writing unit tests. This offers a more detailed discussion of different types of unit tests and patterns of unit test development.

Chapter 5

A discussion of unit testing of GUI applications. This chapter explains how to build and test GUI objects following the smart object model.

Chapter 6

A description of the details of the usage and architecture of JUnit for Java.

Chapter 7

A description of the details of the usage and architecture of CppUnit for C++.

Chapter 8

A description of the details of the usage and architecture for NUnit for .NET.

Chapter 9

A description of the details of the usage and architecture of PyUnit for Python.

Chapter 10

A description of the details of the usage and architecture of XMLUnit for XML.

Chapter 11

A list of additional resources for unit test frameworks and related topics.

Appendix A

The C++ version of the simple unit test framework from Chapter 2.

Appendix B

A detailed class reference for JUnit’s key package junit.framework.

Appendix C

A detailed class reference for CppUnit.

Glossary

A list of definitions for important technical terms used in this book.

Conventions Used in This Book

The following typographical conventions are used in this book:

Plain text

Indicates regular text and descriptions.

Constant width

Indicates commands, methods, attributes, data types, class names, or the output from commands. It also shows the actual source code.

Italic

Indicates new terms where they are defined, pathnames, file directories, filenames, and Internet names, such as email addresses, and URLs.

Constant Width Bold

Indicates source code that is being emphasized for your attention.

Code in this book is formatted as shown here to distinguish it from the rest of the text. Code examples begin with the filename where the code resides.

            MyClass.java
public class MyClass {

    myMethod( ) {
        int id = 3;
    }

}

Using Code Examples

This book is here to help you get your job done. In general, you may use the code in this book in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing a CD-ROM of examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission.

We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, and ISBN. For example: "Unit Test Frameworks, by Paul Hamill. Copyright 2005 O’Reilly Media, Inc., 0-596-00689-6.”

If you feel your use of code examples falls outside fair use or the permission given above, feel free to contact us at .

How to Contact Us

Please address comments and questions concerning this book to the publisher:

O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
(800) 998-9938 (in the United States or Canada)
(707) 829-0515 (international or local)
(707) 829-0104 (fax)

We have a web page for this book, where we list errata, examples, and any additional information. You can access this page at:

http://www.oreilly.com/catalog/unitest/

To comment or ask technical questions about this book, send email to:

For more information about our books, conferences, Resource Centers, and the O’Reilly Network, see our web site at:

http://www.oreilly.com

Acknowledgments

My sincere thanks go out to my reviewers: Ron Jeffries, James Newkirk, Philip Plumlee, J. B. Rainsberger, Simon Robbie, and Anthony Williams. Their shared experience and advice was incredibly useful and encouraging. This book could not have been completed without their help.

This book is built on the work of software pioneers. Kent Beck is the original author of the xUnit architecture in the form of SmalltalkUnit. Ward Cunningham, Kent Beck, and Ron Jeffries are the formulators of the Extreme Programming methodology, which led to many of the test driven development practices described in this book. Erich Gamma and Kent Beck ported SmalltalkUnit to Java to create JUnit, the most widely used and extended unit test framework. Many individual developers created and contributed to the different versions of xUnit, which are classic examples of open source software, built by the collective efforts of the software development community. The fingerprints of these talented engineers are all over the material covered by this book.

Get Unit Test Frameworks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.