Foundations of Software Engineering

Kenneth M. Anderson <kena@cs.colorado.edu>

Lecture 26: Test Driven Design

Credit where Credit is Due

Goals for this Lecture

Test-Driven Design (TDD)

Writing Test Cases First

Example (I)

Example (II)

Example (III)

Example (IV)

Example (V)

Example (VI)

TDD Life Cycle (I)

TDD Life Cycle (II)

Background: Multi-Currency Money

Starting From Scratch

First Test

What’s Next?

Update Testing List

Dollar Class, v. 0.1

public class Dollar {
    public Dollar(int amount) {
    }

    public void times(int multiplier) {
    }
    
    public int amount;
}

Now our test compiles, but fails

Too Slow?

Make the Test Pass

Refactoring

Dollar Class, v. 0.2

public class Dollar {
    public Dollar(int amount) {
    	this.amount = amount;
    }

    public void times(int multiplier) {
    	amount = amount * multiplier;
    }
    
    public int amount;
}

Now our test compiles and passes, and we didn't have to “cheat”!

One loop complete!

Dollar Side Effects

Next Test Case

public void testMultiplication() {
    Dollar five = new Dollar(5);
    Dollar product = five.times(2);
    assertEquals(10, product.amount);
    product = five.times(3);
    assertEquals(15, product.amount);
    assertEquals(5, five.amount);
}

Test Fails

Pass the Test

Discussion of the Example

Principles of TDD (I)

Principles of TDD (II)

Principles of TDD (III)

Summary

Reflections

Experience

Testing Frameworks