Home > Store

xUnit Test Patterns: Refactoring Test Code

Register your product to gain access to bonus material or receive a coupon.

xUnit Test Patterns: Refactoring Test Code

Book

  • Sorry, this book is no longer in print.
Not for Sale

eBook (Watermarked)

  • Your Price: $51.19
  • List Price: $63.99
  • Includes EPUB, MOBI, and PDF
  • About eBook Formats
  • This eBook includes the following formats, accessible from your Account page after purchase:

    ePub EPUB The open industry format known for its reflowable content and usability on supported mobile devices.

    MOBI MOBI The eBook format compatible with the Amazon Kindle and Amazon Kindle applications.

    Adobe Reader PDF The popular standard, used most often with the free Adobe® Reader® software.

    This eBook requires no passwords or activation to read. We customize your eBook by discreetly watermarking it with your name, making it uniquely yours.

Description

  • Copyright 2007
  • Dimensions: 7" x 9-1/4"
  • Pages: 944
  • Edition: 1st
  • Book
  • ISBN-10: 0-13-149505-4
  • ISBN-13: 978-0-13-149505-0

Automated testing is a cornerstone of agile development. An effective testing strategy will deliver new functionality more aggressively, accelerate user feedback, and improve quality. However, for many developers, creating effective automated tests is a unique and unfamiliar challenge.

xUnit Test Patterns is the definitive guide to writing automated tests using xUnit, the most popular unit testing framework in use today. Agile coach and test automation expert Gerard Meszaros describes 68 proven patterns for making tests easier to write, understand, and maintain. He then shows you how to make them more robust and repeatable--and far more cost-effective.

Loaded with information, this book feels like three books in one. The first part is a detailed tutorial on test automation that covers everything from test strategy to in-depth test coding. The second part, a catalog of 18 frequently encountered "test smells," provides trouble-shooting guidelines to help you determine the root cause of problems and the most applicable patterns. The third part contains detailed descriptions of each pattern, including refactoring instructions illustrated by extensive code samples in multiple programming languages.

Topics covered include

  • Writing better tests--and writing them faster
  • The four phases of automated tests: fixture setup, exercising the system under test, result verification, and fixture teardown
  • Improving test coverage by isolating software from its environment using Test Stubs and Mock Objects
  • Designing software for greater testability
  • Using test "smells" (including code smells, behavior smells, and project smells) to spot problems and know when and how to eliminate them
  • Refactoring tests for greater simplicity, robustness, and execution speed

This book will benefit developers, managers, and testers working with any agile or conventional development process, whether doing test-driven development or writing the tests last. While the patterns and smells are especially applicable to all members of the xUnit family, they also apply to next-generation behavior-driven development frameworks such as RSpec and JBehave and to other kinds of test automation tools, including recorded test tools and data-driven test tools such as Fit and FitNesse.


Visual Summary of the Pattern Language 
Foreword

Preface

Acknowledgments

Introduction

Refactoring a Test

PART I: The Narratives
Chapter 1 A Brief Tour
Chapter 2 Test Smells
Chapter 3 Goals of Test Automation
Chapter 4 Philosophy of Test Automation
Chapter 5 Principles of Test Automation
Chapter 6 Test Automation Strategy
Chapter 7 xUnit Basics
Chapter 8 Transient Fixture Management
Chapter 9 Persistent Fixture Management
Chapter 10 Result Verification
Chapter 11 Using Test Doubles
Chapter 12 Organizing Our Tests
Chapter 13 Testing with Databases
Chapter 14 A Roadmap to Effective Test Automation
PART II: The Test Smells 
Chapter 15 Code Smells
Chapter 16 Behavior Smells
Chapter 17 Project Smells
PART III: The Patterns 
Chapter 18 Test Strategy Patterns
Chapter 19 xUnit Basics Patterns
Chapter 20 Fixture Setup Patterns
Chapter 21 Result Verification Patterns
Chapter 22 Fixture Teardown Patterns
Chapter 23 Test Double Patterns
Chapter 24 Test Organization Patterns
Chapter 25 Database Patterns
Chapter 26 Design-for-Testability Patterns
Chapter 27 Value Patterns
PART IV: Appendixes
Appendix A Test Refactorings  
Appendix B xUnit Terminology 
Appendix C xUnit Family Members
Appendix D Tools
Appendix E Goals and Principles
Appendix F Smells, Aliases, and Causes
Appendix G Patterns, Aliases, and Variations
Glossary
References 

Index 


Sample Content

Online Sample Chapters

xUnit Test Patterns: Test Double Patterns

xUnit Test Patterns: Goals of Test Automation

Downloadable Sample Chapter

Download Refactoring a Test

Download Chapter 23: Test Double Patterns

Table of Contents

Visual Summary of the Pattern Language xvii

Foreword xix

Preface xxi

Acknowledgments xxvi

Introduction xxix

Refactoring a Test xlv

PART I: The Narratives 1

Chapter 1 A Brief Tour 3

About This Chapter 3

The Simplest Test Automation Strategy That Could Possibly Work 3

Development Process 4

Customer Tests 5

Unit Tests 6

Design for Testability 7

Test Organization 7

What's Next? 8

Chapter 2 Test Smells 9

About This Chapter 9

An Introduction to Test Smells 9

What's a Test Smell? 10

Kinds of Test Smells 10

What to Do about Smells? 11

A Catalog of Smells 12

The Project Smells 12

The Behavior Smells 13

The Code Smells 16

What's Next? 17

Chapter 3 Goals of Test Automation 19

About This Chapter 19

Why Test? 19

Economics of Test Automation 20

Goals of Test Automation 21

Tests Should Help Us Improve Quality 22

Tests Should Help Us Understand the SUT 23

Tests Should Reduce (and Not Introduce) Risk 23

Tests Should Be Easy to Run 25

Tests Should Be Easy to Write and Maintain 27

Tests Should Require Minimal Maintenance as

the System Evolves Around Them 29

What's Next? 29

Chapter 4 Philosophy of Test Automation 31

About This Chapter 31

Why Is Philosophy Important? 31

Some Philosophical Differences 32

Test First or Last? 32

Tests or Examples? 33

Test-by-Test or Test All-at-Once? 33

Outside-In or Inside-Out? 34

State or Behavior Verification? 36

Fixture Design Upfront or Test-by-Test? 36

When Philosophies Differ 37

My Philosophy 37

What's Next? 37

Chapter 5 Principles of Test Automation 39

About This Chapter 39

The Principles 39

What's Next? 48

Chapter 6 Test Automation Strategy 49

About This Chapter 49

What's Strategic? 49

Which Kinds of Tests Should We Automate? 50

Per-Functionality Tests 50

Cross-Functional Tests 52

Which Tools Do We Use to Automate Which Tests? 53

Test Automation Ways and Means 54

Introducing xUnit 56

The xUnit Sweet Spot 58

Which Test Fixture Strategy Do We Use? 58

What Is a Fixture? 59

Major Fixture Strategies 60

Transient Fresh Fixtures 61

Persistent Fresh Fixtures 62

Shared Fixture Strategies 63

How Do We Ensure Testability? 65

Test Last--At Your Peril 65

Design for Testability--Upfront 65

Test-Driven Testability 66

Control Points and Observation Points 66

Interaction Styles and Testability Patterns 67

Divide and Test 71

What's Next? 73

Chapter 7 xUnit Basics 75

About This Chapter 75

An Introduction to xUnit 75

Common Features 76

The Bare Minimum 76

Defining Tests 76

What's a Fixture? 78

Defining Suites of Tests 78

Running Tests 79

Test Results 79

Under the xUnit Covers 81

Test Commands 82

Test Suite Objects 82

xUnit in the Procedural World 82

What's Next? 83

Chapter 8 Transient Fixture Management 85

About This Chapter 85

Test Fixture Terminology 86

What Is a Fixture? 86

What Is a Fresh Fixture? 87

What Is a Transient Fresh Fixture? 87

Building Fresh Fixtures 88

In-line Fixture Setup 88

Delegated Fixture Setup 89

Implicit Fixture Setup 91

Hybrid Fixture Setup 93

Tearing Down Transient Fresh Fixtures 93

What's Next? 94

Chapter 9 Persistent Fixture Management 95

About This Chapter 95

Managing Persistent Fresh Fixtures 95

What Makes Fixtures Persistent? 95

Issues Caused by Persistent Fresh Fixtures 96

Tearing Down Persistent Fresh Fixtures 97

Avoiding the Need for Teardown 100

Dealing with Slow Tests 102

Managing Shared Fixtures 103

Accessing Shared Fixtures 103

Triggering Shared Fixture Construction 104

What's Next? 106

Chapter 10 Result Verification 107

About This Chapter 107

Making Tests Self-Checking 107

Verify State or Behavior? 108

State Verification 109

Using Built-in Assertions 110

Delta Assertions 111

External Result Verification 111

Verifying Behavior 112

Procedural Behavior Verification 113

Expected Behavior Specification 113

Reducing Test Code Duplication 114

Expected Objects 115

Custom Assertions 116

Outcome-Describing Verification Method 117

Parameterized and Data-Driven Tests 118

Avoiding Conditional Test Logic 119

Eliminating "if" Statements 120

Eliminating Loops 121

Other Techniques 121

Working Backward, Outside-In 121

Using Test-Driven Development to

Write Test Utility Methods 122

Where to Put Reusable Verification Logic? 122

What's Next? 123

Chapter 11 Using Test Doubles 125

About This Chapter 125

What Are Indirect Inputs and Outputs? 125

Why Do We Care about Indirect Inputs? 126

Why Do We Care about Indirect Outputs? 126

How Do We Control Indirect Inputs? 128

How Do We Verify Indirect Outputs? 130

Testing with Doubles 133

Types of Test Doubles 133

Providing the Test Double 140

Configuring the Test Double 141

Installing the Test Double 143

Other Uses of Test Doubles 148

Endoscopic Testing 149

Need-Driven Development 149

Speeding Up Fixture Setup 149

Speeding Up Test Execution 150

Other Considerations 150

What's Next? 151

Chapter 12 Organizing Our Tests 153

About This Chapter 153

Basic xUnit Mechanisms 153

Right-Sizing Test Methods 154

Test Methods and Testcase Classes 155

Testcase Class per Class 155

Testcase Class per Feature 156

Testcase Class per Fixture 156

Choosing a Test Method Organization Strategy 158

Test Naming Conventions 158

Organizing Test Suites 160

Running Groups of Tests 160

Running a Single Test 161

Test Code Reuse 162

Test Utility Method Locations 163

TestCase Inheritance and Reuse 163

Test File Organization 164

Built-in Self-Test 164

Test Packages 164

Test Dependencies 165

What's Next? 165

Chapter 13 Testing with Databases 167

About This Chapter 167

Testing with Databases 167

Why Test with Databases? 168

Issues with Databases 168

Testing without Databases 169

Testing the Database 171

Testing Stored Procedures 172

Testing the Data Access Layer 172

Ensuring Developer Independence 173

Testing with Databases (Again!) 173

What's Next? 174

Chapter 14 A Roadmap to Effective Test Automation 175

About This Chapter 175

Test Automation Difficulty 175

Roadmap to Highly Maintainable Automated Tests 176

Exercise the Happy Path Code 177

Verify Direct Outputs of the Happy Path 178

Verify Alternative Paths 178

Verify Indirect Output Behavior 179

Optimize Test Execution and Maintenance 180

What's Next? 181

PART II: The Test Smells 183

Chapter 15 Code Smells 185

Obscure Test 186

Conditional Test Logic 200

Hard-to-Test Code 209

Test Code Duplication 213

Test Logic in Production 217

Chapter 16 Behavior Smells 223

Assertion Roulette 224

Erratic Test 228

Fragile Test 239

Frequent Debugging 248

Manual Intervention 250

Slow Tests 253

Chapter 17 Project Smells 259

Buggy Tests 260

Developers Not Writing Tests 263

High Test Maintenance Cost 265

Production Bugs 268

PART III: The Patterns 275

Chapter 18 Test Strategy Patterns 277

Recorded Test 278

Scripted Test 285

Data-Driven Test 288

Test Automation Framework 298

Minimal Fixture 302

Standard Fixture 305

Fresh Fixture 311

Shared Fixture 317

Back Door Manipulation 327

Layer Test 337

Chapter 19 xUnit Basics Patterns 347

Test Method 348

Four-Phase Test 358

Assertion Method 362

Assertion Message 370

Testcase Class 373

Test Runner 377

Testcase Object 382

Test Suite Object 387

Test Discovery 393

Test Enumeration 399

Test Selection 403

Chapter 20 Fixture Setup Patterns 407

In-line Setup 408

Delegated Setup 411

Creation Method 415

Implicit Setup 424

Prebuilt Fixture 429

Lazy Setup 435

Suite Fixture Setup 441

Setup Decorator 447

Chained Tests 454

Chapter 21 Result Verification Patterns 461

State Verification 462

Behavior Verification 468

Custom Assertion 474

Delta Assertion 485

Guard Assertion 490

Unfinished Test Assertion 494

Chapter 22 Fixture Teardown Patterns 499

Garbage-Collected Teardown 500

Automated Teardown 503

In-line Teardown 509

Implicit Teardown 516

Chapter 23 Test Double Patterns 521

Test Double 522

Test Stub 529

Test Spy 538

Mock Object 544

Fake Object 551

Configurable Test Double 558

Hard-Coded Test Double 568

Test-Specific Subclass 579

Chapter 24 Test Organization Patterns 591

Named Test Suite 592

Test Utility Method 599

Parameterized Test 607

Testcase Class per Class 617

Testcase Class per Feature 624

Testcase Class per Fixture 631

Testcase Superclass 638

Test Helper 643

Chapter 25 Database Patterns 649

Database Sandbox 650

Stored Procedure Test 654

Table Truncation Teardown 661

Transaction Rollback Teardown 668

Chapter 26 Design-for-Testability Patterns 677

Dependency Injection 678

Dependency Lookup 686

Humble Object 695

Test Hook 709

Chapter 27 Value Patterns 713

Literal Value 714

Derived Value 718

Generated Value 723

Dummy Object 728

PART IV: Appendixes 733

Appendix A Test Refactorings 735

Appendix B xUnit Terminology 741

Appendix C xUnit Family Members 747

Appendix D Tools 753

Appendix E Goals and Principles 757

Appendix F Smells, Aliases, and Causes 761

Appendix G Patterns, Aliases, and Variations 767

Glossary 785

References 819

Index 835

Preface

Download the Preface

Foreword

Download the Foreword

Index

Download the Index

Introduction

Download the Introduction

Updates

Submit Errata

More Information

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020