xunit Test Patterns Refactoring Test Code Gerard Meszaros r\addison-wesley

Similar documents
Software Systems Architecture

CIRCUITS. Raj Nair Donald Bennett PRENTICE HALL

STRATEGY AND THE BUSINESS LANDSCAPE

SAMPLE NINTH EDITION. Margaret L. Lial American River College. Stanley A. Salzman American River College

Management. Industrial Safety and Heal. Sixth Edition. David W. Rieske. C. Ray Asfahl. University of Arkansas UNIVERSITATSB'.

Computer-Aided Three-Dimensional

Digital Signal Processing Mitra 4th Edition Solution Manual

ngineering and Technology

Is This a Bug or an Obsolete Test?

DOWNLOAD OR READ : THE PAST IS PRESENT PDF EBOOK EPUB MOBI

APPLICATION FOR APPROVAL OF A IENG EMPLOYER-MANAGED FURTHER LEARNING PROGRAMME

PREPARING SYBIL IHRIG EMIL IHRIG

arxiv: v1 [cs.se] 27 Apr 2017

LabVIEW 8" Student Edition

Nilsson Riedel Electric Circuits 8th Edition

Design of Analog CMOS Integrated Circuits

Fundamentals. Collected Papers by DAVID L PARNAS. Edited by Daniel M. Hoffman David M. Weiss ADDISON-WESLEY

Information Technology for

Model Minded Development. George Fairbanks SATURN 3 May 2016

DOWNLOAD OR READ : DIGITAL SIGNAL PROCESSING SIGNALS SYSTEMS AND FILTERSHERR ALLER DINGE PDF EBOOK EPUB MOBI

Activity 6: Playing Elevens

Course Outline Department of Computing Science Faculty of Science

Contents. The software development problem The XP solution The JUnit testing framework. 2002, W. Pree 2

Environment The Science Behind The Stories 4th Edition

TABLE OF CONTENTS DECLARATION DEDICATION ACKNOWLEDGEMENTS ABSTRACT ABSTRAK LIST OF TABLES LIST OF FIGURES LIST OF TERMINOLOGY LIST OF APPENDICES

Digital Signal Processing System Design: LabVIEW-Based Hybrid Programming

Principles of Measurement Systems

Policy-Based RTL Design

Understanding Digital Signal Processing Pearsoncmg

INSTRUMENTATION AND CONTROL SYSTEMS SECOND EDITION

Phase-Locked Loop Engineering Handbook for Integrated Circuits

A FRAMEWORK FOR PERFORMING V&V WITHIN REUSE-BASED SOFTWARE ENGINEERING

Mastering Autodesk Navisworks 2013

CIRCUITS, SYSTEMS, AND SIGNALS FOR BIOENGINEERS: A MATLAB-BASED INTRODUCTION

Phase-Locked Loops. Roland E. Best. Me Graw Hill. Sixth Edition. Design, Simulation, and Applications

UC Berkeley, EECS Department EECS 40/42/100 Lab PRJ1: Project 1 Light Gate UID:

Module Role of Software in Complex Systems

The Complete Guide to Game Audio

ELECTRIC CIRCUITS. Third Edition JOSEPH EDMINISTER MAHMOOD NAHVI

Integrity Monitoring? New thinking in the approach to Subsea IMMR. Dr Karl Woods, Snr Subsea Reliability Engineer 22/2/2017

McGraw-Hill Irwin DIGITAL SIGNAL PROCESSING. A Computer-Based Approach. Second Edition. Sanjit K. Mitra

Power Systems Modelling and Fault Analysis

Digital transformation in the Catalan public administrations

Integrated Circuit Design for High-Speed Frequency Synthesis

Digital Systems Testing And Testable Design Solutions

Academic Lesson Plan

@3 [fijl)~ 8: ~ 0 ~~ ~~~ C

Introducing Functional Qualification

Towards Integrated System and Software Modeling for Embedded Systems

Microelectronic Circuits

Applied Digital Signal Processing Theory And Practice Solutions

UNIT IV SOFTWARE PROCESSES & TESTING SOFTWARE PROCESS - DEFINITION AND IMPLEMENTATION

State-Based Modeling to Support the Evolution and Maintenance of Safety- Critical Software Product Lines

UNIT-III LIFE-CYCLE PHASES

Debugging a Boundary-Scan I 2 C Script Test with the BusPro - I and I2C Exerciser Software: A Case Study

IMGD 1001: Programming Practices; Artificial Intelligence

Introduction to Robotics in CIM Systems

Implementation of Automated Testing in Matlab using MLUnit Tool

GAME PRODUCTION HANDBOOK Second Edition

System analysis and signal processing

bus waveforms transport delta and simulation

PRACTICAL RF SYSTEM DESIGN

Seddik Bacha Iulian Munteanu Antoneta Iuliana Bratcu. Power Electronic Converters. and Control. Modeling. with Case Studies.

IMGD 1001: Programming Practices; Artificial Intelligence

CONTENTS FOREWORD... VII ACKNOWLEDGMENTS... IX CONTENTS... XI LIST OF FIGURES... XVII LIST OF TABLES... XIX LIST OF ABBREVIATIONS...

CHAPTER 1 INTRODUCTION...

FINDING AND MERGING DUPLICATES IN FAMILY TREE

Digital Excellence Study

AAPSilver System Performance Validation

Automated Software Engineering Writing Code to Help You Write Code. Gregory Gay CSCE Computing in the Modern World October 27, 2015

INF3430 Clock and Synchronization

SWEN 256 Software Process & Project Management

Crucible: A FHIR Testing Tool

While entry is at the discretion of the centre it would be beneficial if candidates had the following IT skills:

ATHABASCA UNIVERSITY CAN TEST DRIVEN DEVELOPMENT IMPROVE POKER ROBOT PERFORMANCE? EDWARD SAN PEDRO. An essay submitted in partial fulfillment

Verification of Digitally Calibrated Analog Systems with Verilog-AMS Behavioral Models

wxpython in Action NOEL RAPPIN ROBIN DÜNN MANNING Greenwich (74 w. long.)

Warren J. Smith Chief Scientist, Consultant Rockwell Collins Optronics Carlsbad, California

Advances and Perspectives in Health Information Standards

Computer-Aided Manufacturing

Gaming Development. Resources

Indiana K-12 Computer Science Standards

Experience Optional: The Australian CFO Route to the Top

SPICE for Power Electronics and Electric Power

Developing a Versatile Audio Synthesizer TJHSST Senior Research Project Computer Systems Lab

Mobile Broadband Multimedia Networks

Unit 6.5 Text Adventures

Applied Digital Signal Processing Manolakis Solution Manual File Type

IBM Software Group. Mastering Requirements Management with Use Cases Module 2: Introduction to RMUC

FRBR Primary Entities

AutoCAD Electrical Fundamentals with NFPA Standards

Contents. 1 Introduction. 2 System-Level Electrostatic Discharge (ESD) and Electrical Fast Transient. 3 Electromagnetic Interference

CERN (The European Laboratory for Particle Physics)

Research Statement. 1. Research Overview. 2. Current Research Agenda

Electronic Circuits Systems John D Ryder Charles M

STATE UNIVERSITY OF NEW YORK COLLEGE OF TECHNOLOGY CANTON, NEW YORK COURSE OUTLINE MECH 223 INTRODUCTION TO COMPUTER NUMERICAL CONTROL

Domain Understanding and Requirements Elicitation

SECTION LOW VOLTAGE ACTIVE HARMONIC FILTER SYSTEM NEMA 1 ENCLOSED

ACADEMIC LESSON PLAN

Benford's Law. Theory, the General Law of Relative Quantities, and Forensic Fraud Detection Applications. Alex Ely Kossovsky.

Transcription:

xunit Test Patterns Refactoring Test Code Gerard Meszaros r\addison-wesley Upper Saddle River, NJ Boston Indianapolis San Francisco New York Toronto Montreal London Munich Paris Madrid Capetown Sydney Tokyo Singapore Mexico City

Contents Visual Summary of the Pattern Language Foreword Preface Acknowledgments Introduction Refactoring a Test xvii xix xxi xxvii xxix 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 vii

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 Strategie? 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 61 Divide and Test 71 What's Next? 73 er 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 H. 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

VISUAL SUMMARY OF THE PATTERN LANGUAGE 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. Smeils, Aliases, and Causes 761 Appendix G. Patterns, Aliases, and Variations 767 Glossary 785 References 819 Index 835