What readers are saying about
Pragmatic Unit Testing. . .
“This book starts out with a nice introduction discussing
what unit testing is as well as why we should do it. I like the
anecdotes peppered throughout the book illustrating the
point of why one should bother. . . . I also really liked the
analogies you use. It puts the code into a real-world context.”
Sharee L. Johnson,
Project Lead, Applications Development
“I wish I had a copy back when I started doing test-first
development as part of Extreme Programming.”
Al Koscielny, Software Developer
“I’m not totally new to testing, but I’ve struggled with many
aspects of it. I think this book does a good job of bringing
those along who are completely new to unit testing, but still
has enough advanced material to assist those of us who have
dabbled in testing and floundered once we’ve hit obstacles.”
Andrew Thompson,
Consultant, Greenbrier & Russel
“When I’m on a project that needs to be doing unit testing
better (which is often the case), I’d like to have this book
available as a simple reference to suggest to the team.”
Bobby Woolf, Consulting I/T Specialist,
IBM Software Services for Websphere
“I am a firm believer in unit testing and I would want all
team members I work with to be religiously practicing the
techniques recommended in this book. I think there is a lot
of good, practical information in this book that any
professional software engineer should be incorporating into
their daily work.”
James J. O’Connor III,
Lead System Design Engineer
Pragmatic Unit Testing
in C# with NUnit
Andy Hunt
Dave Thomas
The Pragmatic Bookshelf
Raleigh, North Carolina Dallas, Texas
Many of the designations used by manufacturers and sellers to distinguish
their products are claimed as trademarks. Where those designations appear
in this book, and The Pragmatic Programmers, LLC was aware of a trademark
claim, the designations have been printed in initial capital letters or in all
capitals.
Every precaution was taken in the preparation of this book. However, the
publisher assumes no responsibility for errors or omissions, or for damages
that may result from the use of information (including program listings) con-
tained herein.
Our Pragmatic courses, workshops and other products can help you and your
team create better software and have more fun. For more information, as well
as the latest Pragmatic titles, please visit us at:
http://www.pragmaticprogrammer.com
Copyright
c
2003, 2004 The Pragmatic Programmers, LLC. All rights re-
served. No part of this publication may be reproduced, stored in a retrieval
system, or transmitted, in any form, or by any means, electronic, mechani-
cal, photocopying, recording, or otherwise, without the prior consent of the
publisher.
Printed in the United States of America.
ISBN 0-9745140-2-0
Text printed on acid-free paper.
First printing, March 2004
Version: 2004-3-5
Contents
About the Starter Kit viii
Preface x
1 Introduction 1
1.1 Coding With Confidence . . . . . . . . . . . . . . 2
1.2 What is Unit Testing? . . . . . . . . . . . . . . . 3
1.3 Why Should I Bother with Unit Testing? . . . . 4
1.4 What Do I Want to Accomplish? . . . . . . . . . 5
1.5 How Do I Do Unit Testing? . . . . . . . . . . . . 7
1.6 Excuses For Not Testing . . . . . . . . . . . . . . 7
1.7 Roadmap . . . . . . . . . . . . . . . . . . . . . . 12
2 Your First Unit Tests 13
2.1 Planning Tests . . . . . . . . . . . . . . . . . . . 14
2.2 Testing a Simple Method . . . . . . . . . . . . . 15
2.3 Running Tests with NUnit . . . . . . . . . . . . 16
2.4 Running the Example . . . . . . . . . . . . . . . 22
2.5 More Tests . . . . . . . . . . . . . . . . . . . . . 26
3 Writing Tests in NUnit 27
3.1 Structuring Unit Tests . . . . . . . . . . . . . . . 27
3.2 NUnit Asserts . . . . . . . . . . . . . . . . . . . . 29
3.3 NUnit Framework . . . . . . . . . . . . . . . . . 31
3.4 NUnit Test Selection . . . . . . . . . . . . . . . . 33
3.5 NUnit Custom Asserts . . . . . . . . . . . . . . . 40
3.6 NUnit and Exceptions . . . . . . . . . . . . . . . 41
3.7 Temporarily Ignoring Tests . . . . . . . . . . . . 42
评论0
最新资源