135
Test Driven Development Embrace Uncertainty Naresh Jain Twitter: @nashjain http://blogs.agilefaqs.com Licensed Under Creative Commons by Naresh Jain 1 Friday, October 2, 2009

TDD Overview

Embed Size (px)

DESCRIPTION

Million Feet Overview of Test Driven Development. (not a very technical presentation).

Citation preview

Page 1: TDD Overview

Test Driven DevelopmentEmbrace Uncertainty

Naresh JainTwitter: @nashjain

http://blogs.agilefaqs.com

Licensed Under Creative Commons by Naresh Jain1Friday, October 2, 2009

Page 2: TDD Overview

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 3: TDD Overview

Add a Test

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 4: TDD Overview

Add a Test

Run the Test

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 5: TDD Overview

Add a Test

Run the TestPass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 6: TDD Overview

Add a Test

Run the Test

Fail

Pass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 7: TDD Overview

Add a Test

Run the Test

Make a little change

Fail

Pass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 8: TDD Overview

Add a Test

Run the Test

Make a little change

Run the Test

Fail

Pass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 9: TDD Overview

Add a Test

Run the Test

Make a little change

Run the Test

Fail

Fail

Pass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 10: TDD Overview

Add a Test

Run the Test

Make a little change

Run the Test

Fail

Pass

Fail

Pass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 11: TDD Overview

Add a Test

Run the Test

Make a little change

Run the Test

Refactor

Fail

Pass

Fail

Pass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 12: TDD Overview

Add a Test

Run the Test

Make a little change

Run the Test

Refactor

Fail

Pass

Fail

Pass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 13: TDD Overview

Add a Test

Run the Test

Make a little change

Run the Test

Refactor

Fail

Pass

Fail

Pass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 14: TDD Overview

Add a Test

Run the Test

Make a little change

Run the Test

Refactor

Fail

Pass

Fail

Pass

TDD RhythmTest, Code, Refactor

2Friday, October 2, 2009

Page 15: TDD Overview

But....

3Friday, October 2, 2009

Page 16: TDD Overview

ROI?

Is there a p

roblem here?

Extra Work?

How to Start?Who? When?Productivity?

3Friday, October 2, 2009

Page 17: TDD Overview

Continuum

4Friday, October 2, 2009

Page 18: TDD Overview

5Friday, October 2, 2009

Page 19: TDD Overview

New to Agile

Post Agile

5Friday, October 2, 2009

Page 20: TDD Overview

New to Agile

Post Agile

New to TDDExpert TDDPractitioners

5Friday, October 2, 2009

Page 21: TDD Overview

Me

6Friday, October 2, 2009

Page 22: TDD Overview

7Friday, October 2, 2009

Page 23: TDD Overview

Mumbai8Friday, October 2, 2009

Page 24: TDD Overview

9Friday, October 2, 2009

Page 25: TDD Overview

10Friday, October 2, 2009

Page 26: TDD Overview

We don’t travel like this

11Friday, October 2, 2009

Page 27: TDD Overview

We are getting there...

12Friday, October 2, 2009

Page 28: TDD Overview

Tech Talks!

13Friday, October 2, 2009

Page 29: TDD Overview

14Friday, October 2, 2009

Page 30: TDD Overview

15Friday, October 2, 2009

Page 31: TDD Overview

16Friday, October 2, 2009

Page 32: TDD Overview

Warmup Scenario

17Friday, October 2, 2009

Page 33: TDD Overview

18Friday, October 2, 2009

Page 34: TDD Overview

Pick one scenario and in relation to your scenario, what are the specific observable results that will tell you that the activity has been successfully completed?

18Friday, October 2, 2009

Page 35: TDD Overview

Pick one scenario and in relation to your scenario, what are the specific observable results that will tell you that the activity has been successfully completed?

Going out for Movie (THX sound and Digital projection)

18Friday, October 2, 2009

Page 36: TDD Overview

Pick one scenario and in relation to your scenario, what are the specific observable results that will tell you that the activity has been successfully completed?

Going out for Movie (THX sound and Digital projection)

Going out for meal (one veg.)

18Friday, October 2, 2009

Page 37: TDD Overview

Pick one scenario and in relation to your scenario, what are the specific observable results that will tell you that the activity has been successfully completed?

Going out for Movie (THX sound and Digital projection)

Going out for meal (one veg.)

Going shopping ($50)[10 Minutes]

18Friday, October 2, 2009

Page 38: TDD Overview

Pick one scenario and in relation to your scenario, what are the specific observable results that will tell you that the activity has been successfully completed?

Going out for Movie (THX sound and Digital projection)

Going out for meal (one veg.)

Going shopping ($50)[10 Minutes]

Present back to the group your findings. [3 minutes per group]

18Friday, October 2, 2009

Page 39: TDD Overview

Acceptance Criteria and Tests

A critical piece of Agile

19Friday, October 2, 2009

Page 40: TDD Overview

Acceptance CriteriaIs a set of conditions that the Story must meet for it to be accepted as complete

Is typically provided by the customer or product owner.

Is not a replacement for conversation.

Is one of the results of the conversation

Acceptance Criteria are NOT tests

20Friday, October 2, 2009

Page 41: TDD Overview

Given [Precondition]

When [Actor + Action]

Then [Observable Result]

21Friday, October 2, 2009

Page 42: TDD Overview

Acceptance Criteria

Examples (data + scenarios)

Acceptance Tests

22Friday, October 2, 2009

Page 43: TDD Overview

Acceptance Criteria

Examples (data + scenarios)

Acceptance Tests

+

22Friday, October 2, 2009

Page 44: TDD Overview

DemoRoman Numerals to Decimal Conversion Example

23Friday, October 2, 2009

Page 45: TDD Overview

Analysis

Design

Implementation

DFDERD

DDST

1 May 1 Nov1 Jul 1 Sep

24Friday, October 2, 2009

Page 46: TDD Overview

Are we building the right product?

Are we building the product right?

Business Facing

Technology/Implementation Facing

25Friday, October 2, 2009

Page 47: TDD Overview

Brian Marick’s Test Categorization

Business Facing

Technology/Implementation Facing

Supp

orts

Pro

gram

min

gC

ritique product

26Friday, October 2, 2009

Page 48: TDD Overview

Business Facing

Technology/Implementation Facing

Dri

ves

Dev

elop

men

t Critique product

It Helps to think...

27Friday, October 2, 2009

Page 49: TDD Overview

Business Facing

Technology/Implementation Facing

Dri

ves

Dev

elop

men

t Critique product

Unit Testing

It Helps to think...

27Friday, October 2, 2009

Page 50: TDD Overview

Business Facing

Technology/Implementation Facing

Dri

ves

Dev

elop

men

t Critique product

Unit Testing

Acceptance Testing Low-fi prototypes

It Helps to think...

27Friday, October 2, 2009

Page 51: TDD Overview

Business Facing

Technology/Implementation Facing

Dri

ves

Dev

elop

men

t Critique product

Unit Testing

Acceptance Testing Low-fi prototypes

Exploratory TestingUI and Usability Testing

It Helps to think...

27Friday, October 2, 2009

Page 52: TDD Overview

Business Facing

Technology/Implementation Facing

Dri

ves

Dev

elop

men

t Critique product

Unit Testing

Acceptance Testing Low-fi prototypes

Exploratory TestingUI and Usability Testing

Performance TestingSystem Tests

It Helps to think...

27Friday, October 2, 2009

Page 53: TDD Overview

Acceptance Test Driven

28Friday, October 2, 2009

Page 54: TDD Overview

Acceptance Test Driven

Story

28Friday, October 2, 2009

Page 55: TDD Overview

Acceptance Test Driven

Story

Acceptance Criteria

28Friday, October 2, 2009

Page 56: TDD Overview

Acceptance Test DrivenIteration

Story

Acceptance Criteria

28Friday, October 2, 2009

Page 57: TDD Overview

Acceptance Test DrivenIteration

Automated Acceptance

Tests

Story

Acceptance Criteria

28Friday, October 2, 2009

Page 58: TDD Overview

Acceptance Test DrivenIteration

Automated Acceptance

Tests

Story

Acceptance Criteria

Automated Unit Test

28Friday, October 2, 2009

Page 59: TDD Overview

Acceptance Test DrivenIteration

Automated Acceptance

Tests

AutomatedAcceptance

Tests

Story

Acceptance Criteria

Automated Unit Test

28Friday, October 2, 2009

Page 60: TDD Overview

Acceptance Test DrivenIteration

Automated Acceptance

Tests

AutomatedAcceptance

Tests

Story

Acceptance Criteria

ExploratoryTesting

Automated Unit Test

28Friday, October 2, 2009

Page 61: TDD Overview

Acceptance Test DrivenIteration

Automated Acceptance

Tests

AutomatedAcceptance

Tests

Story

Acceptance Criteria

Acceptance Criteria

ExploratoryTesting

Automated Unit Test

28Friday, October 2, 2009

Page 62: TDD Overview

Acceptance Test DrivenIteration

Automated Acceptance

Tests

AutomatedAcceptance

Tests

Story

Acceptance Criteria

Acceptance Criteria

ExploratoryTesting

Automated UI Tests

Automated Unit Test

28Friday, October 2, 2009

Page 63: TDD Overview

Acceptance Test DrivenIteration

Automated Acceptance

Tests

AutomatedAcceptance

Tests

Story

Acceptance Criteria

Acceptance Criteria

ExploratoryTesting

Automated UI Tests

TESTS

PERFORMENCE

Automated Unit Test

28Friday, October 2, 2009

Page 64: TDD Overview

Mike Cohn’s Testing Pyramid

29Friday, October 2, 2009

Page 65: TDD Overview

Mike Cohn’s Testing Pyramid

GUITests

29Friday, October 2, 2009

Page 66: TDD Overview

Mike Cohn’s Testing Pyramid

GUITests

Small in NumberTools: Selenium, Sahi, Watir, Abbot, Frankenstein

29Friday, October 2, 2009

Page 67: TDD Overview

Mike Cohn’s Testing Pyramid

AcceptanceTests

GUITests

Small in NumberTools: Selenium, Sahi, Watir, Abbot, Frankenstein

29Friday, October 2, 2009

Page 68: TDD Overview

Mike Cohn’s Testing Pyramid

AcceptanceTests

GUITests

Small in NumberTools: Selenium, Sahi, Watir, Abbot, Frankenstein

At least one per storyTools: Fit, FitNesse, RSpec, Cucumber

29Friday, October 2, 2009

Page 69: TDD Overview

Mike Cohn’s Testing Pyramid

Unit Tests

AcceptanceTests

GUITests

Small in NumberTools: Selenium, Sahi, Watir, Abbot, Frankenstein

At least one per storyTools: Fit, FitNesse, RSpec, Cucumber

29Friday, October 2, 2009

Page 70: TDD Overview

Mike Cohn’s Testing Pyramid

Unit Tests

AcceptanceTests

GUITests

Small in NumberTools: Selenium, Sahi, Watir, Abbot, Frankenstein

At least one per storyTools: Fit, FitNesse, RSpec, Cucumber

At least one per class or moduleTools: xUnit, TestNG

29Friday, October 2, 2009

Page 71: TDD Overview

Business Tier

View

Data Store

Acceptance Tests

UI

Model and Presenter

30Friday, October 2, 2009

Page 72: TDD Overview

Why care about TDD?

31Friday, October 2, 2009

Page 73: TDD Overview

32Friday, October 2, 2009

Page 74: TDD Overview

# of Lines of Code?

33Friday, October 2, 2009

Page 75: TDD Overview

# of Story Points (estimates)?

34Friday, October 2, 2009

Page 76: TDD Overview

35Friday, October 2, 2009

Page 77: TDD Overview

# of Features developed?

35Friday, October 2, 2009

Page 78: TDD Overview

# of Features tested?

35Friday, October 2, 2009

Page 79: TDD Overview

# of Features deployed?

35Friday, October 2, 2009

Page 80: TDD Overview

# of Features used actively?

35Friday, October 2, 2009

Page 81: TDD Overview

What about Maintenance?

36Friday, October 2, 2009

Page 82: TDD Overview

Stay away from (time hungry) debugger

37Friday, October 2, 2009

Page 83: TDD Overview

Manual (monkey) Checking by

Developers and Tester

38Friday, October 2, 2009

Page 84: TDD Overview

Maintain Focus

39Friday, October 2, 2009

Page 85: TDD Overview

Reduce Wastage: Hand Overs

40Friday, October 2, 2009

Page 86: TDD Overview

Communication

41Friday, October 2, 2009

Page 87: TDD Overview

Living, up-to-date specification

42Friday, October 2, 2009

Page 88: TDD Overview

Communicate Design Decisions

43Friday, October 2, 2009

Page 89: TDD Overview

Learning:Listen to your code

44Friday, October 2, 2009

Page 90: TDD Overview

Baby Steps:Slow down and think

45Friday, October 2, 2009

Page 91: TDD Overview

Confidence

46Friday, October 2, 2009

Page 92: TDD Overview

Testable Code by Design+ Safety Net

47Friday, October 2, 2009

Page 93: TDD Overview

Loosely Coupled Design

48Friday, October 2, 2009

Page 94: TDD Overview

Refactoring

49Friday, October 2, 2009

Page 95: TDD Overview

Test Driven DesignBefore you take the plunge

50Friday, October 2, 2009

Page 96: TDD Overview

Evolutionary Design

51Friday, October 2, 2009

Page 97: TDD Overview

System Metaphor

52Friday, October 2, 2009

Page 98: TDD Overview

Thin Slicing

53Friday, October 2, 2009

Page 99: TDD Overview

Interaction Design

54Friday, October 2, 2009

Page 100: TDD Overview

Tracer Bullets

55Friday, October 2, 2009

Page 101: TDD Overview

Avatars of TDDRule of Diversity

Distrust all claims for “One True Way”

56Friday, October 2, 2009

Page 102: TDD Overview

Business Facing

Technology/Implementation Facing

Dri

ves

Dev

elop

men

t Critique product

Outside In

Inside Out

Brian Marick’s Test Categorization

57Friday, October 2, 2009

Page 103: TDD Overview

Veterinarian Information System

58Friday, October 2, 2009

Page 104: TDD Overview

Outside In: ATDDFitnesse Document:Assertions: 17 right, 0 wrong, 0 ignored, 0 exceptions

com.vis.billing.fixtures.PaidCashBill

account details

account number patient name owner name

1001 Fluffy Dave Atkins

procedure detailsname costRoutine Office Visit 250

Rabies Vaccination 50

billaccount number? owner name? patient name? total? paid?1001 Dave Atkins Fluffy 300 false

procedure details on the billname costRoutine Office Visit 250

Rabies Vaccination 50

pay Cash

patient name? owner name? account number? bill no? payment method? amount paid?

Fluffy Dave Atkins 1001 1 Cash 300

check paid true

check total 0

59Friday, October 2, 2009

Page 105: TDD Overview

Outside In: Output

60Friday, October 2, 2009

Page 106: TDD Overview

ATDD: Another ExampleAcceptance test:class FluffyTest < Test::Unit::TestCase def test_examination_and_shots vet = Veterinarian.new clinic = Clinic.new "Main Line health" dave = Owner.new "Dave" fluffy = Patient.new "Fluffy" dave.has fluffy visit = clinic.visit(fluffy, vet) do |v| v.procedure "Rabies vaccination", 50 end invoice = visit.invoice assert_equal invoice.to_s, <<-INVOICE Routine visit: $200 Rabies vaccination: $50 Total: $250 INVOICE receipt = clinic.pay_for visit, 100 assert_equal receipt.to_s, <<-RECEIPT Paid $100 for Routine visit Paid $0 for Rabies vaccination Outstanding: $150 RECEIPT endend

61Friday, October 2, 2009

Page 107: TDD Overview

Outside In: Output

62Friday, October 2, 2009

Page 108: TDD Overview

Inside Out: Unit TDDpublic class ClinicTest { private Clinic clinic = new Clinic();

@Test public void amountOnTheReceiptShouldMatchBillableAmount() throws Exception { Billable billable = new Billable() { public int totalAmount() { return 0; } }; Account dave = new Account(101, "Dave");

Receipt rcpt = clinic.payCash(dave, billable); assertEquals("Amount on receipt does match procedure cost", billable.totalAmount(), rcpt.getAmount()); }

@Test public void customerPayesBillableAmountForCashTransaction() throws Exception { final int billableAmount = 56; class AmountCharged { int charged; }; final AmountCharged charged = new AmountCharged();

Billable billable = new Billable() { public int totalAmount() { return billableAmount; } };

Account dave = new Account(101, "Dave") { public void charge(int amount) { charged.charged = amount; } };

clinic.payCash(dave, billable); assertEquals("Account is not charged billable amount", billableAmount,charged.charged); }}

public class BillableTest {

private static final Account daveSAccount = new Account(101, "Dave"); private static final List<Service> services = new ArrayList<Service>();

@Test public void totalBillableAmountShouldBeZeroIfNoServicesAreProvided() { Billable bill = new Bill(daveSAccount, services); assertEquals("Total amount is not Zero", 0, bill.totalAmount()); }

@Test public void totalBillableAmountShouldBeTotalOfEachServiceProvided() { services.add(new Procedure("Rabies Vaccination", 250)); services.add(new Procedure("Regular office Visit", 50));

Billable bill = new Bill(daveSAccount, services);

assertEquals("Total Amount is not 300", 300, bill.totalAmount()); }

@After public void cleanUp() { services.clear(); }}

63Friday, October 2, 2009

Page 109: TDD Overview

Inside Out: Output

64Friday, October 2, 2009

Page 110: TDD Overview

Inside Out: Another Examplepublic class ChargeAccountForServices { private static final Billable bill = createMock(Billable.class); private static final Accountable account = createMock(Accountable.class); private static final Clinic clinic = new Clinic();

@Before public void setUp() { reset(account); reset(bill); }

@Test public void shouldMakePaymentsAgainstAnAccount() { account.paid(bill); replay(account); clinic.pay(300, bill, account); verify(account); }

@Test public void shouldHaveZeroAmountDueOnReceiptIfCompletePaymentIsMade() { expect(bill.amount()).andReturn(300); replay(bill); Receipt receipt = clinic.pay(300, bill, account); verify(bill); assertEquals(300, receipt.amount()); assertEquals(0, receipt.amountDue()); }

@Test public void shouldDisplayAmountDueOnTheReceiptIfIncompletePaymentIsMade() { expect(bill.amount()).andReturn(500); replay(bill); Receipt receipt = clinic.pay(300, bill, account); verify(bill); assertEquals(300, receipt.amount()); assertEquals(200, receipt.amountDue()); }}

65Friday, October 2, 2009

Page 111: TDD Overview

public class CreateBillForClientAccount { private static final List<Service> services = new ArrayList<Service>(); private static final Accountable account = createMock(Accountable.class); private Bill bill;

@Before public void setUp() { reset(account); }

@Test public void shouldThrowExceptionIfAccountIsNotDueForPayment() { expect(account.isPaymentDue()).andReturn(false); replay(account); try { new Bill(account, null); } catch (NoPaymentDueException e) { // expected } verify(account); }

@After public void cleanUp() { services.clear(); }

@Test public void shouldCreateABillWithTheTotalCostOfAllTheServices() { IMocksControl control = createControl(); Service rabiesVaccination = control.createMock(Service.class); Service routineVisit = control.createMock(Service.class); services.add(rabiesVaccination); services.add(routineVisit);

expect(account.isPaymentDue()).andReturn(true); expect(account.unpaidServices()).andReturn(services); bill();

expect(rabiesVaccination.cost()).andReturn(250); expect(routineVisit.cost()).andReturn(50);

control.replay();

assertEquals(300, bill.amount()); control.verify(); }

private void bill() throws NoPaymentDueException { replay(account); bill = new Bill(account, null); verify(account); }}

66Friday, October 2, 2009

Page 112: TDD Overview

Inside Out: Output

67Friday, October 2, 2009

Page 113: TDD Overview

Coffee Vending MachineHow would you develop it?

68Friday, October 2, 2009

Page 114: TDD Overview

Feedback

69Friday, October 2, 2009

Page 115: TDD Overview

70Friday, October 2, 2009

Page 116: TDD Overview

71Friday, October 2, 2009

Page 117: TDD Overview

72Friday, October 2, 2009

Page 118: TDD Overview

Visualizing ProficiencyIndustrial Logic’s

e-learningAutomated Analysis of Lab Exerciseshttp://industriallogic.com/elearning/

73Friday, October 2, 2009

Page 119: TDD Overview

74Friday, October 2, 2009

Page 120: TDD Overview

75Friday, October 2, 2009

Page 121: TDD Overview

76Friday, October 2, 2009

Page 122: TDD Overview

77Friday, October 2, 2009

Page 123: TDD Overview

78Friday, October 2, 2009

Page 124: TDD Overview

79Friday, October 2, 2009

Page 125: TDD Overview

Tools & Practices

80Friday, October 2, 2009

Page 126: TDD Overview

Pair Programming

81Friday, October 2, 2009

Page 127: TDD Overview

Continuous Integration

82Friday, October 2, 2009

Page 128: TDD Overview

83Friday, October 2, 2009

Page 129: TDD Overview

Coding Convention+

Static Code Analysis

84Friday, October 2, 2009

Page 130: TDD Overview

Checkstyle

85Friday, October 2, 2009

Page 131: TDD Overview

Code Coverage

86Friday, October 2, 2009

Page 132: TDD Overview

Cobertura

87Friday, October 2, 2009

Page 133: TDD Overview

DB Management

88Friday, October 2, 2009

Page 134: TDD Overview

BeringMigrate4j

89Friday, October 2, 2009

Page 135: TDD Overview

Questions?

[email protected]

90Friday, October 2, 2009