3 elements of tdd

48
3 Elements of TDD Todd Wei

Upload: weidagang

Post on 20-Dec-2014

755 views

Category:

Technology


6 download

DESCRIPTION

Agile Tour 2010 Chengdu presentation

TRANSCRIPT

Page 1: 3 Elements of TDD

3 Elements of TDD

Todd Wei

Page 2: 3 Elements of TDD

Outline Overview of TDD Test First Development Iterative Incremental Development Refactor

Page 3: 3 Elements of TDD

TDD

Pass the test case

RefactorWrite a

failing test case

Page 4: 3 Elements of TDD

3 Elements of TDD

TFD

IIDRefactor

Page 5: 3 Elements of TDD

Outline Overview of TDD Test First Development Iterative Incremental Development Refactor

Page 6: 3 Elements of TDD

Test First Development

Write a Failing Test Case

Pass the Test Case

Page 7: 3 Elements of TDD

Why Write Test First? Testability Code Coverage Quick Feedback Simple Design Decoupling Bug Free …

?

Decoupling

Testability

Coverage

Page 8: 3 Elements of TDD

Why Write Test First? Testability Code Coverage Quick Feedback Simple Design Decoupling Bug Free …

Specification

Decoupling

Testability

Coverage

Page 9: 3 Elements of TDD

Test Case as Specification

“Specification by Example”–Martin Fowler

Page 10: 3 Elements of TDD

What’s Specification?

“A specification is an explicit set of requirements to be satisfied by a material, product, or service.” -- Wikipedia

Page 11: 3 Elements of TDD

What’s Specification?

User Vendor

Specification

Page 12: 3 Elements of TDD

Specification is in Every Level

Page 13: 3 Elements of TDD

Where does specification come from?

Requirement

Specification

Requirement Analysis

Page 14: 3 Elements of TDD

Test Case as Specification

Requirement

Document

Requirement

Test Case

Traditional

TDD

Page 15: 3 Elements of TDD

Software Specification Forms

Specificatio

n

DocumentDesign by ContractTest Case

Page 16: 3 Elements of TDD

Document

General

Not Precise

No Feedback

Not Reliable

Page 17: 3 Elements of TDD

Design by Contract Contract

Precondition Postcondition Invariant

Precise

Quick Feedback

Abstraction

Limited Functionality

Page 18: 3 Elements of TDD

Test Case

Precise

Quick Feedback

User Friendly

Not General

Page 19: 3 Elements of TDD

How to Write Good Test Case

Follow requirements

Think of as black box

Construct a scenario

Verify external behavior

Page 20: 3 Elements of TDD

Example: Stack

FILO

Page 21: 3 Elements of TDD

Bad Test Style

push()

data

pop()

Function/procedure/method and data are the focuses of test

Page 22: 3 Elements of TDD

Bad Test Style

Page 23: 3 Elements of TDD

Good Test Style

push()

data

pop()

Stack

Feature is the focus of test, e.g. FILO

Page 24: 3 Elements of TDD

Good Test Style

Page 25: 3 Elements of TDD

Bad vs Good

Bad Good

Behavior

Scenario

Black Box

Requirement

State

Procedure

White Box

Implementation

Page 26: 3 Elements of TDD

4 Steps on Writing Good Test Case

Follow requirements

Think of as black box

Construct a scenario

Verify external behavior

Page 27: 3 Elements of TDD

Outline Overview of TDD Test First Development Iterative Incremental Development Refactor

Page 28: 3 Elements of TDD

Iterative Incremental Development

Pass the test case

Write a failing test

case

TDD = TFD + IID + Refactor

Page 29: 3 Elements of TDD

Waterfall

Requirement Analysis

Design

Implementation

TestMaintenance

Page 30: 3 Elements of TDD

Waterfall

Phase1

Phase2

Phase3 …

Page 31: 3 Elements of TDD

Layer Hierarchy

Business Logic Layer

Presentation Layer

Data Access Layer

Page 32: 3 Elements of TDD

Agile: Iterative Incremental Development

Feature1

Feature2

Feature3

Feature1.1

Feature1.2

Feature1.3

Page 33: 3 Elements of TDD

Feature Hierarchy

Page 34: 3 Elements of TDD

Example: multi-dimensional objectName Project Expertise Degree Gender

Allen Project1 Develop Master Male

Rebecca Project1 Test Bachelor Female

Kay Project2 Develop Bachelor Female

David Project2 Test Master Male

… … … … …

Company

Develop

Project1

Project2

Test

Company

Project1

Develop Test

Project2

Page 35: 3 Elements of TDD

Function Cohesion

“Parts of a module are grouped because they all contribute to a single well-defined task of the module” -- Wikipedia

Page 36: 3 Elements of TDD

What does unit mean?

Unit vs Atom

Page 37: 3 Elements of TDD

What does unit mean?

Hierarchy vs Single Responsibility

Page 38: 3 Elements of TDD

What should be tested in unit test?

Do unit test on all levels that can be automated!

Page 39: 3 Elements of TDD

Outline Overview of TDD Test First Development Iterative Incremental Development Refactor

Page 40: 3 Elements of TDD

TDD

Pass the test case

RefactorWrite a

failing test case

TDD = TFD + IID + Refactor

Page 41: 3 Elements of TDD

Software Entropy

Software complexity, change cost increase exponentially as the project goes on

Page 42: 3 Elements of TDD

Chaos is not agile

Page 43: 3 Elements of TDD

Refactor

Refactoring is a disciplined technique for altering its internal structure without changing its external behavior

Page 44: 3 Elements of TDD

Refactor: Bring Order to Chaos

Page 45: 3 Elements of TDD

Refactor with TFD

TFD facilitates refactoring and ensure no functionality is broken with regression tests

Page 46: 3 Elements of TDD

Refactor with IID

Version1

Version1.1

Version2

Refactor to a stable intermediate version before adding new feature

Page 47: 3 Elements of TDD

Summary

Pass the test

case

Refactor

Write a failing test case

TFD

IIDRefactor

Page 48: 3 Elements of TDD

Question & Answer

Thanks!