The Art of Unit Testing, Second Edition
with examples in C#
Roy Osherove
Forewords by Michael Feathers and Robert C. Martin
  • November 2013
  • ISBN 9781617290893
  • 296 pages
  • printed in black & white

This book is something special. Th e chapters build on each other to a startling accumulation of depth. Get ready for a treat.

From the Foreword by Robert C. Martin,

The Art of Unit Testing, Second Edition guides you step by step from writing your first simple tests to developing robust test sets that are maintainable, readable, and trustworthy. You'll master the foundational ideas and quickly move to high-value subjects like mocks, stubs, and isolation, including frameworks such as Moq, FakeItEasy and Typemock Isolator. You'll explore test patterns and organization, working with legacy code, and even "untestable" code. Along the way, you'll learn about integration testing and techniques and tools for testing databases and other technologies.

Table of Contents show full

foreword to the second edition

foreword to the first edition



about this book

about the cover illustration

Part 1 Getting started

1. The basics of unit testing

1.1. Defining unit testing, step by step

1.2. Properties of a good unit test

1.3. Integration tests

1.4. What makes unit tests good

1.5. A simple unit test example

1.6. Test-driven development

1.7. The three core skills of successful TDD

1.8. Summary

2. A first unit test

2.1. Frameworks for unit testing

2.2. Introducing the LogAn project

2.3. First steps with NUnit

2.4. Writing your first test

2.5. Refactoring to parameterized tests

2.6. More NUnit attributes

2.7. Testing results that are system state changes instead of return values

2.8. Summary

Part 2 Core techniques

3. Using stubs to break dependencies

3.1. Introducing stubs

3.2. Identifying a filesystem dependency in LogAn

3.3. Determining how to easily test LogAnalyzer

3.4. Refactoring your design to be more testable

3.5. Variations on refactoring techniques

3.6. Overcoming the encapsulation problem

3.7. Summary

4. Interaction testing using mock objects

4.1. Value-based vs. state-based vs. interaction testing

4.2. The difference between mocks and stubs

4.3. A simple handwritten mock example

4.4. Using a mock and a stub together

4.5. One mock per test

4.6. Fake chains: stubs that produce mocks or other stubs

4.7. The problems with handwritten mocks and stubs

4.8. Summary

5. Isolation (mocking) frameworks

5.1. Why use isolation frameworks?

5.2. Dynamically creating a fake object

5.3. Simulating fake values

5.5. Current isolation frameworks for .NET

5.6. Advantages and traps of isolation frameworks

5.7. Summary

6. Digging deeper into isolation frameworks

6.1. Constrained and unconstrained frameworks

6.2. Values of good isolation frameworks

6.3. Features supporting future-proofing and usability

6.4. Isolation framework design antipatterns

6.5. Summary

Part 3 The test code

7. Test hierarchies and organization

7.1. Automated builds running automated tests

7.2. Mapping out tests based on speed and type

7.3. Ensuring tests are part of source control

7.4. Mapping test classes to code under test

7.5. Cross-cutting concerns injection

7.6. Building a test API for your application

7.7. Summary

8. The pillars of good unit tests

8.1. Writing trustworthy tests

8.2. Writing maintainable tests

8.3. Writing readable tests

8.4. Summary

Part 4 Design and process

9. Integrating unit testing into the organization

9.1. Steps to becoming an agent of change

9.2. Ways to succeed

9.3. Ways to fail

9.4. Influence factors

9.5. Tough questions and answers

9.6. Summary

10. Working with legacy code

10.1. Where do you start adding tests?

10.2. Choosing a selection strategy

10.3. Writing integration tests before refactoring

10.4. Important tools for legacy code unit testing

10.5. Summary

11. Design and testability

11.1. Why should I care about testability in my design?

11.2. Design goals for testability

11.3. Pros and cons of designing for testability

11.4. Alternatives to designing for testability

11.5. Example of a hard-to-test design

11.6. Summary

11.7. Additional resources

Appendix A: Tools and frameworks


About the book

You know you should be unit testing, so why aren't you doing it? If you're new to unit testing, if you find unit testing tedious, or if you're just not getting enough payoff for the effort you put into it, keep reading.

The Art of Unit Testing, Second Edition guides you step by step from writing your first simple unit tests to building complete test sets that are maintainable, readable, and trustworthy. You'll move quickly to more complicated subjects like mocks and stubs, while learning to use isolation (mocking) frameworks like Moq, FakeItEasy, and Typemock Isolator. You'll explore test patterns and organization, refactor code applications, and learn how to test "untestable" code. Along the way, you'll learn about integration testing and techniques for testing with databases.

What's inside

  • Create readable, maintainable, trustworthy tests
  • Fakes, stubs, mock objects, and isolation (mocking) frameworks
  • Simple dependency injection techniques
  • Refactoring legacy code

About the reader

The examples in the book use C#, but will benefit anyone using a statically typed language such as Java or C++.

About the author

Roy Osherove has been coding for over 15 years, and he consults and trains teams worldwide on the gentle art of unit testing and test-driven development. His blog is at

combo $44.99 pBook + eBook
eBook $35.99 pdf + ePub + kindle

FREE domestic shipping on three or more pBooks

The best way to learn unit testing from what is now a classic in the field.

Raphael Faria, LG Electronics

Teaches you the philosophy as well as the nuts and bolts for effective unit testing.

Pradeep Chellappan, Microsof

When my team members ask me how to write unit tests the right way, I simply answer: Get this book!

Alessandro Campeis, Vimar SpA

The single best resource on unit testing.

Kaleb Pederson, Next IT Corporation