没有合适的资源?快使用搜索试试~ 我知道了~
首页The Art Of Unit Testing
.NET Unit Testing Table of Contents Part 1 Getting started Chapter 1 The basics of unit testing Chapter 2 The first unit test Part 2 Core techniques Chapter 3 Using Stubs to break dependencies Chapter 4 Interaction testing using Mock Objects Chapter 5 Mock Object frameworks Part 3 The test code Chapter 6 Test hierarchies and organization Chapter 7 The pillars of good tests Part 4 Design and process Chapter 8 Integrating unit testing into the organization Chapter 9 Working with legacy code Appendices Appendix A Design and testability Appendix B Extra tools and frameworks
资源详情
资源评论
资源推荐

1

3
Table of Contents
Part1 Gettingstarted
Chapter 1 The basics of unit testing
Chapter 2 The first unit test
Part2 Coretechniques
Chapter 3 Using Stubs to break dependencies
Chapter 4 Interaction testing using Mock Objects
Chapter 5 Mock Object frameworks
Part3 Thetestcode
Chapter 6 Test hierarchies and organization
Chapter 7 The pillars of good tests
Part4 Designandprocess
Chapter 8 Integrating unit testing into the organization
Chapter 9 Working with legacy code
Appendices
Appendix A Design and testability
Appendix B Extra tools and frameworks

4
1
The basics of unit testing
One of the biggest failed projects I worked on had unit tests. Or so I thought. I was leading a group of
programmers to create a billing application, and we were doing it in a fully test-driven manner – writing the test,
then writing the code, seeing the test fail, making the test pass, refactor, rinse, repeat.
The first few months of the project were great; things were looking up, and we had tests that proved that our
code worked. As time went by, requirements changed, and we were forced to change our code to fit those new
requirements. Whenever we changed the code, tests broke and we had to fix them – the code was still working,
but the tests we wrote were so brittle that any little change in our code broke them, even though the code was
working just fine. It became a daunting task to change our code in a class or a method for fear of changing all the
unit tests involved with that unit being tested.
Worse yet, some tests became unusable because the people who wrote them had left the project and no one
knew how to maintain the tests, or what they were testing. The names we gave our unit test methods were not
clear enough. We had tests relying on other tests. We ended up throwing away most of the tests less than 6
months into the project.
It was a miserable failure because we let the tests we wrote do more harm than good – they were taking too
much time to maintain and understand than they were saving us in the long run. So we stopped using them. I
moved on to other projects, where we did a better job writing our unit tests, and even had some great successes
using them, saving huge amounts of debugging and integration time.
Ever since that first project that failed, I’ve been compiling best practices for unit tests and using them on the
next project. Every time I get involved in a new project, I find a few more best practices. A solid naming guideline
is just one of those. Understanding how to write unit tests, and making them maintainable, readable and trust-
worthy is what this book is about– no matter what language or Integrated Development Environment (IDE) you
work with.
This book will cover the basics of writing a unit test, then move on to the basics of Interaction testing, and
from then we’ll move to best practices for writing, managing and maintaining unit tests in the real world.
1.1 Unit testing - classic definition
Unit testing in software development is not a new concept. It’s been floating around since the early days of the
programming language Smalltalk in the 1970s and proves itself time and time again as one of the best ways a
developer can improve the quality of code while gaining a deeper understanding of the functional requirements of a
class or method.
Kent Beck is the person who introduced the concept of unit testing in Smalltalk. The concept he created has carried
on into many programming languages today, which has made unit testing an extremely useful practice in software
programming. Before we get too far, we need to define unit testing better. I will start with the classic definition
most of us have heard a million times.

5
UNIT TEST – THE CLASSIC DEFINITION
A unit test is a piece of a code (usually a method) that invokes another piece of code and checks the correctness
of some assumptions afterward. If the assumptions turn out to be wrong, the unit test has failed. A “unit” is a
method or function.
The piece of code being tested is often called SUT (System Under Test). The piece of code that tests the SUT
will usually reside in a Test Method. This classic definition, while technically correct, is hardly enough to get us
started down a path where we can better ourselves as developers. Chances are you already know this and are
getting bored even reading this definition again, as it appears practically in any web site or book that discusses unit
testing.
Don’t worry; in this book, I’ll take you beyond the classic definition of unit testing by addressing issues not
mentioned in it: Maintainability, Readability, Correctness and more. However, precisely because you probably
might already be familiar with the classic definition, it gives us a shared knowledge base from which to extend the
idea of a unit test into something with more value than is currently defined.
No matter what programming language you are using, one of the hardest aspects of defining a unit test is
defining what is meant by a “good” one.
1.1.1 Defining a “good” unit test
I firmly believe that there’s no point in writing a bad unit test. If you’re going to write a unit test badly, you may as
well not write it all and save yourself the trouble it will cause down the road with maintainability and time
schedules. Defining what a good unit test is, is the first step we can do to make sure we don’t start off with the
wrong notion of what we’re trying to write.
Most people who try to unit test their code either give up at some point or don’t actually perform unit tests.
Instead, they either rely on system and integration tests to be performed much later in the lifecycle of the product
they are developing or resort to manually testing the code via custom test applications or actually using the end
product they are developing to invoke their code.
To succeed, it is essential that you not only have a technical definition of a unit test, but that you describe
the properties of a good unit test. To understand what a good unit test is, we’ll need look at what we do today to
understand what developers have been doing so far in a software project.
If you haven’t been doing unit tests, how did you make sure that the code works?
1.1.2 We’ve all written unit tests
You may be surprised to learn this, but you’ve already implemented some types of unit testing on your own. Have
you ever met a developer who has not tested the code they wrote before letting it out from under their hands?
Well, neither have I.
It might have been a console application that called the various methods of a class or component, some specially
created Winform or Webform UI that checks the functionality of that class or component, or even manual tests that
were run by performing various actions within the real application’s UI to test the end functionality. The end result
is that the developer is certain, to a degree, that the code works well enough to give it away to someone else.
Figure 1.1 shows how most developers test their code.
剩余168页未读,继续阅读













安全验证
文档复制为VIP权益,开通VIP直接复制

评论2