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

This preview shows page 1-2-3-4-29-30-31-32-59-60-61-62 out of 62 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 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 62 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 62 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

© University of Colorado, 2010Test-Driven DevelopmentKenneth M. AndersonUniversity of Colorado, BoulderCSCI 5828 — Lecture 22 — 04/01/201012Credit where Credit is Due (I)Some of the material for this lecture is taken from “Test-Driven Development” by Kent Beckas such some of this material is copyright © Addison Wesley, 2003In addition, some material for this lecture is taken from “Agile Software Development: Principles, Patterns, and Practices” by Robert C. Martinas such some materials is copyright © Pearson Education, Inc., 20033Credit where Credit is Due (II)Finally, one of the examples is inspired by the Roman Numerals example that is featured in Dive into Python 3 <http://diveintopython3.org/> by Mark Pilgrim.The slides devoted to that example are thus distributed using the following license: <http://creativecommons.org/licenses/by-sa/3.0/>.Side NotePointer to a Podcast on the topic of Test Driven Development<http://faceoffshow.com/2009/03/31/episode-10-test-driven-development/>4GoalsReview material from Chapter 8 of Pilone & MilesTest-Driven DevelopmentTerminologyConceptsTechniquesTools5Test-Driven DevelopmentAn agile practice that asserts that testing is a fundamental part of software developmentRather than thinking of testing as something that occurs after implementation, we want to think of it as something that occurs BEFORE and DURING implementationIndeed, done properly, testing can DRIVE implementationThe result, increased confidence when performing other tasks such as fixing bugs, refactoring, or reimplementing parts of your software system67TestimonialOn Monday, September 8, 2003, at 03:44 PM, a former student wrote:> Dr. Anderson ->> I hope you don't mind hearing from former students :) Remember me > from Object Oriented Analysis and Design last spring? I'm now happily > graduated and working in the so-called 'Real World' (yikes).>> I just wanted to give you another testimony on the real-life use of > test driven development. My co-workers are stunned that I am actually > using something at work that I learned at school (well, not really, > but they like to tease). For a new software parsing tool I'm > developing, I decided to use TDD to develop it and it is making my > life so easy right now to test new changes.>> Anyways, I just thought of you and your class when I decided to use > this and I wanted to let you know.>> I hope that you are doing well. Best of luck on this new semester.8Test FirstThe definition of test-driven development:All production code is written to make failing test cases passTerminologyProduction code is code that is deployed to end users and used in their “production environments” that is there day to day workImplicationsWhen developing software, we write a test case first, watch it fail, then write the simplest code to make it pass; repeatExample (I)Consider writing a program to score the game of bowlingpublic class TestGame extends TestCase {public void testOneThrow() {Game g = new Game();g.addThrow(5);assertEquals(5, g.getScore());}}When you compile this program, the test “fails” because the Game class does not yet exist. But:You have defined two methods on the class that you want to useYou are designing this class from a client’s perspective9Example (II)You would now write the Game classpublic class Game {public void addThrow(int pins) {}public int getScore() {return 0;}}The code now compiles but the test will still fail:getScore() returns 0 not 5In Test-Driven Design, Beck recommends taking small, simple stepsSo, we get the test case to compile before we get it to pass10Example (III)Once we confirm that the test still fails, we would then write the simplest code to make the test case pass; that would bepublic class Game {public void addThrow(int pins) {}public int getScore() {return 5;}}The test case now passes! ☺11Example (IV)But, this code is not very useful! Lets add a new test casepublic class TestGame extends TestCase {public void testOneThrow() {Game g = new Game();g.addThrow(5);assertEquals(5, g.getScore());}public void testTwoThrows() {Game g = new Game();g.addThrow(5); g.addThrow(4);assertEquals(9, g.getScore());}}The first test passes, but the second case fails (since 9 ≠ 5)This code is written using JUnit; it uses reflection to invoke tests automatically12Example (V)We have duplication of information between the first test and the Game classIn particular, the number 5 appears in both placesThis duplication occurred because we were writing the simplest code to make the test passNow, in the presence of the second test case, this duplication does more harm than goodSo, we must now refactor the code to remove this duplication13Example (VI)public class Game {private int score = 0;public void addThrow(int pins) {score += pins;}public int getScore() {return score;}}14Both tests now pass. Progress!Example (VII)But now we to make additional progress, we add another test case to the TestGame class…public void testSimpleSpare() {Game g = new Game()g.addThrow(3); g.addThrow(7); g.addThrow(3);assertEquals(13, g.scoreForFrame(1));assertEquals(16, g.getScore());}…We’re back to the code not compiling due to scoreForFrame()We’ll need to add a method body for this method and give it the simplest implementation that will make all three of our tests cases pass1516TDD Life CycleThe life cycle of test-driven development isQuickly add a testRun all tests and see the new one failMake a simple changeRun all tests and see them all passRefactor to remove duplicationThis cycle is followed until you have met your goal;17TDD Life Cycle, continuedKent Beck likes to perform TDD using a testing framework, such as JUnit.Within such frameworksfailing tests are indicated with a “red bar”passing tests are shown with a “green bar”As such, the TDD life cycle is sometimes described as“red bar/green bar/refactor”JUnit: Red Bar...When a test fails:You see a red barFailures/Errors are listedClicking on a failure displays more detailed information about what went wrong1819Example Background:Multi-Currency Money Lets design a system that will allow us to perform financial transactions with money that may be in different currenciese.g. if we know that the exchange rate from Swiss Francs to U.S. Dollars is 2 to 1 then we can calculate expressions like5 USD + 10 CHF = 10 USDor5 USD + 10 CHF = 20 CHF20Starting From ScratchLets start developing such an exampleHow do we start?TDD recommends writing a list of things we


View Full Document

CU-Boulder CSCI 5828 - Test-Driven Development

Documents in this Course
Drupal

Drupal

31 pages

Deadlock

Deadlock

23 pages

Deadlock

Deadlock

23 pages

Deadlock

Deadlock

22 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?