DOC PREVIEW
CU-Boulder CSCI 6448 - Test-Driven Development

This preview shows page 1-2-3 out of 8 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 8 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 29: Test-DrivenDevelopmentKenneth M. AndersonObject-Oriented Analysis and DesignCSCI 6448 - Spring Semester, 2003April 22, 2003 © University of Colorado, 2003 2Credit where Credit is Due Some of the material for this lecture istaken from “Test-Driven Development”by Kent Beck; as such some of thismaterial is copyright © Addison Wesley,2003April 22, 2003 © University of Colorado, 2003 3Goals for this lecture Introduce the concept of Test-DrivenDevelopment (TDD) Present an exampleApril 22, 2003 © University of Colorado, 2003 4Test-Driven Development The idea is simple No production code is written except tomake a failing test pass Implication You have to write test cases before youwrite codeApril 22, 2003 © University of Colorado, 2003 5Writing Test Cases First This means that when you first write a testcase, you may be testing code that does notexist And since that means the test case will notcompile, obviously the test case “fails” After you write the skeleton code for the objectsreferenced in the test case, it will now compile, butalso may not pass So, then you write the simplest code that will thenmake the test case passApril 22, 2003 © University of Colorado, 2003 6TDD Life Cycle The life cycle of test-driven development is Quickly add a test Run all tests and see the new one fail Make a simple change Run all tests and see them all pass Refactor to remove duplication This cycle is followed until you have met yourgoal; note that this cycle simply adds testingto the “add functionality; refactor” loop ofrefactoring covered last weekApril 22, 2003 © University of Colorado, 2003 7TDD Life Cycle, continued Kent Beck likes to perform TDD within aTesting Framework, such as JUnit,within such frameworks failing tests are indicated with a “red bar” passing tests are shown with a “green bar” As such, the TDD life cycle issometimes described as “red bar/green bar/refactor”April 22, 2003 © University of Colorado, 2003 8Example Background:Multi-Currency Money Lets design a system that will allow us toperform financial transactions with money thatmay be in different currencies e.g. if we know that the exchange rate from SwissFrancs to U.S. Dollars is 2 to 1 then we cancalculate expressions like 5 USD + 10 CHF = 10 USD or 5 USD + 10 CHF = 20 CHFApril 22, 2003 © University of Colorado, 2003 9Starting From Scratch Lets start developing such an example How do we start? TDD recommends writing a list of things wewant to test This list can take any format, just keep itsimple Example $5 + 10 CHF = $10 if rate is 2:1 $5 * 2 = $10April 22, 2003 © University of Colorado, 2003 10First Test The first test case looks a bit complex, letsstart with the second 5 USD * 2 = 10 USD First, we write a test casepublic void testMultiplication() {Dollar five = new Dollar(5);five.times(2);assertEquals(10, five.amount)}April 22, 2003 © University of Colorado, 2003 11Discussion on Test Casepublic void testMultiplication() {Dollar five = new Dollar(5);five.times(2);assertEquals(10, five.amount)} What benefits does this provide? target class plus some of its interface we are designing the interface of the Dollar class bythinking about how we would want to use it We have made a testable assertion about thestate of that class after we perform a particularsequence of operationsApril 22, 2003 © University of Colorado, 2003 12What’s Next? We need to update our test list The test case revealed some things about Dollarthat we will want to clean up We are representing the amount as an integer, which willmake it difficult to represent values like 1.5 USD; how willwe handle rounding of factional amounts? Dollar.amount is public; violates encapsulation What about side effects?; we first declared our variableas “five” but after we performed the multiplication it nowequals “ten”April 22, 2003 © University of Colorado, 2003 13Update Testing List The New List 5 USD + 10 CHF = 10 USD $5 * 2 = $10 make “amount” private Dollar side-effects? Money rounding? Now, we need to fix the compile errors no class Dollar, no constructor, no method times,no field amountApril 22, 2003 © University of Colorado, 2003 14First version of Dollar Classpublic class Dollar {public Dollar(int amount) {}public void times(int multiplier) {}public int amount;} Now our test compiles and fails!April 22, 2003 © University of Colorado, 2003 15Too Slow? Note: we did the simplest thing to make thetest compile; now we are going to do the simplest thing tomake the test pass Is this process too slow? Yes, as you get familiar with the TDD life cycle youwill gain confidence and make bigger steps No, taking small simple steps avoids mistakes;beginning programmers try to code too muchbefore invoking the compiler; they then spend therest of their time debugging!April 22, 2003 © University of Colorado, 2003 16How do we make the testpass? Here’s one waypublic void times(int multiplier) {amount = 5 * 2;} The test now passes, we received a “greenbar”! Now, we need to “refactor to removeduplication” But where is the duplication? Hint: its between the Dollar class and the test caseApril 22, 2003 © University of Colorado, 2003 17Refactoring To remove the duplication of the testdata and the hard-wired code of thetimes method, we think the following “We are trying to get a 10 at the end ofour test case and we’ve been given a 5in the constructor and a 2 was passedas a parameter to the times method” So, lets hook things upApril 22, 2003 © University of Colorado, 2003 18First version of Dollar Classpublic 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!April 22, 2003 © University of Colorado, 2003 19One loop complete! Before writing the next test case, weupdate our testing list 5 USD + 10 CHF = 10 USD $5 * 2 = $10 make “amount” private Dollar side-effects? Money rounding?April 22, 2003 © University of Colorado, 2003 20One more example Lets address the “Dollar Side-Effects” itemand then move on to general lessons So, lets write the next test case When we called the times operation our variable“five” was


View Full Document

CU-Boulder CSCI 6448 - Test-Driven Development

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Test-Driven Development
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Test-Driven Development and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Test-Driven Development 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?