chapter 21 test-driven development 1cs6359 fall 2011 john cole

13
Chapter 21 Test-Driven Development 1 CS6359 Fall 2011 John Cole

Upload: gervase-fowler

Post on 05-Jan-2016

219 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Chapter 21

Test-Driven Development

1CS6359 Fall 2011 John Cole

Page 2: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Write the Tests First

• Tests are written before the code• Developer writes unit tests for nearly all

production code

CS6359 Fall 2011 John Cole 2

Page 3: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Advantages

• The tests actually get written• Programmer satisfaction leads to more

consistent test writing• Clarification of detailed interface and behavior• Provable, repeatable, automated verification• Confidence to change things

CS6359 Fall 2011 John Cole 3

Page 4: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Example

• To test the Sale class, create a SaleTest class that:– Creates a Sale (the thing to be tested; a fixture)– Adds some line items to it with the makeLineItem

method– Asks for the total and verifies that it is the

expected value

CS6359 Fall 2011 John Cole 4

Page 5: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Pattern of Testing Methods

• Create the fixture• Do some operation you want to test• Evaluate the results• Point: Don’t write all of the tests for Sale first;

write one test method, implement the solution in Sale to make it pass, then repeat

CS6359 Fall 2011 John Cole 5

Page 6: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Using xUnit

• Create a class that extends the TestCase class• Create a separate testing method for each

Sale method you want to test. This will generally be every public method.

CS6359 Fall 2011 John Cole 6

Page 7: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Refactoring

• A structured, disciplined method to rewrite or restructure existing code without changing its external behavior, applying small transformations and re-testing each step.

• Unit tests applied after each step ensure that the changes didn’t cause a problem

CS6359 Fall 2011 John Cole 7

Page 8: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Goals of Refactoring

• Remove duplicate code• Improve clarity• Make long methods shorter• Remove the use of hard-coded literal

constants

CS6359 Fall 2011 John Cole 8

Page 9: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Code Smells

• Duplicated code• Big methods• Class with many instance variables• Class with lots of code• Strikingly similar subclasses• Little or no use of interfaces• High coupling

CS6359 Fall 2011 John Cole 9

Page 10: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Basic Refactorings

• Extract method – Convert a long method into shorter ones by moving part of it into a helper method

• Extract constant – Replace a literal with a constant variable

• Introduce explaining variable – put the results of an expression into a temporary variable with a name that explains its purpose

• Replace constructor call with Factory Method –

CS6359 Fall 2011 John Cole 10

Page 11: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

More Refactorings

• Change method parameters (changes them in the definition, then finds all references)

• Replace using new with a call to a method that returns an object. (Factory pattern)

CS6359 Fall 2011 John Cole 11

Page 12: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Code examplePublic class Die { public static final int MAX=6; public int faceValue; public Die(){ roll(); }Public void roll() { faceValue = (int)((Math.Random() * MAX) + 1; }Public int getFaceValue() { return faceValue; }}

CS6359 Fall 2011 John Cole 12

Page 13: Chapter 21 Test-Driven Development 1CS6359 Fall 2011 John Cole

Another Example

• rollDice on 392

CS6359 Fall 2011 John Cole 13