Test Driven Development
Upcoming SlideShare
Loading in...5
×
 

Test Driven Development

on

  • 3,864 views

Working with Legacy Code (talk for colleagues at Pason Systems in 2003)

Working with Legacy Code (talk for colleagues at Pason Systems in 2003)

Statistics

Views

Total Views
3,864
Views on SlideShare
3,855
Embed Views
9

Actions

Likes
2
Downloads
61
Comments
0

2 Embeds 9

http://www.slideshare.net 7
http://www.guydavis.ca 2

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • This was an in-house seminar I gave for our development team (6 people) at work to help us get over the initial challenges in writing unit tests for our code. <br /> <br /> <br /> <br /> The presentation and discussion only took an hour or so. Then we split up into pairs and worked on adding unit tests to our untested Java, C, and Python code for an afternoon. <br /> <br /> <br /> <br /> We then held an hour long meeting to discuss the outcome of the pair work. Details about test frameworks, what worked, what didn't, etc. were discussed. <br />
  • Reverses the order of typical development. (Where tests are often skipped or ignored at the end) <br /> <br /> <br /> <br /> Quote: <br /> <br /> “If you can't write a test for what you are about to code, then you shouldn't even be thinking about coding.” <br /> <br /> <br /> <br /> <br /> Dramatically narrows the gap between intent and result in software development. Unintended errors are found much sooner, which saves time and cost. <br />
  • 1) Write a test: <br /> <br /> - Think about the task in your mind. <br /> <br /> <br /> - Invent interfaces you wish you had. <br /> <br /> <br /> - Include all the elements needed. <br /> <br /> 2) Make it run: <br /> <br /> - Quickly get the bar green with new test <br /> <br /> <br /> - If can see a solution requiring a while to write, <br /> <br /> <br /> just write it down on To-Do list <br /> <br /> <br /> - Will cover in details approaches for this... <br /> <br /> <br /> - Ignore coding standard, use constants, etc... <br /> <br /> 3) Make it right (refactor): <br /> <br /> - Now remove code duplication <br /> <br /> <br /> - Meet coding standards <br /> <br /> <br /> - Keep the bar green! <br /> <br />
  • Ask that they cover the method/class with unit tests. <br /> <br /> <br /> <br /> Stress the importance of recording the order in which they work. What steps did they follow? <br /> <br /> <br /> <br /> Give them about 5-10 minutes. Answer questions as they work. <br /> <br /> <br /> <br /> Then discuss for 5 minutes: <br /> What tests did they choose and why? <br /> Show my example with 6 tests, other author did 65 <br /> How many is enough? <br />
  • Greedy ordering algorithm: <br /> <br /> “Write some tests and write the methods that allow you to run more tests sooner.” <br /> <br /> <br /> <br /> <br /> Describe task lists: <br /> <br /> OPT task list for main work tasks <br /> <br /> <br /> Pad of paper for things to not forget <br /> <br /> <br /> Evolution task list for TDD coding <br /> <br /> <br /> <br /> <br /> Keeps my ideas for other tests, possible refactorings, etc. Anything that distracts from current test I'm in. <br /> <br /> <br /> <br /> Coverage in industry can be 80-90% <br />
  • <br /> “Code that works” stage <br /> <br />
  • Give example of addition operation on board: <br /> <br /> <br /> <br /> Test: assert(7 == Integer.add(five, two)) <br /> <br /> <br /> <br /> Code: return (5+2); hard-coded <br /> <br /> <br /> <br /> Metaphor of rock climber getting to far above last attachment point <br /> <br /> <br /> <br /> Psychological benefit to seeing bar green is more confidence <br /> <br /> <br /> <br /> Concentrating on a single test means you'll do a better job of it, than juggling two or three at a time. <br />
  • Start with one test: <br /> <br /> assert(7 == Integer.add(five, two)) <br /> <br /> Code: return 7; <br /> <br /> <br /> <br /> Add more asserts to a test case <br /> <br /> assert(4 == Integer.add(two, two)) <br /> <br /> <br /> <br /> <br /> Now must correct implementation to pass new asserts <br /> <br /> <br /> <br /> private int add(int augend, int addend) <br /> { return augend + addend }; <br />
  • Emphasize the power of choice here to go fast or slow during development. <br /> <br /> <br /> <br /> How many times have we seen the same problem occur again in a edit, compile, debug cycle? Unit tests prevent this type of regression. If making the same mistake, then <br />
  • <br /> “Clean code” stage... <br /> <br />
  • See Beck's TDD book for details. <br />
  • See Beck's TDD book for details. <br />
  • First ask what they consider legacy code? <br /> <br /> <br /> <br /> Can be defined as any code without automated unit tests. <br />
  • Methods: <br /> <br /> <br /> <br /> <br /> - debugger to step through <br /> <br /> <br /> - code tracing <br /> <br /> <br /> - runtime logging <br /> <br />
  • <br /> <br />
  • <br /> <br />
  • <br /> <br /> <br /> Method example: parsing a data line of EDR data need array of params and column mapping. Rather than hit the database, I just populate the array with the ones I need manually. <br /> <br /> <br /> <br /> Other ways of breaking external dependencies? <br /> <br /> <br /> <br /> Refactoring... example Extract Method... <br />
  • Notice, I say “handled” error conditions. These coverage tests only test the current functionality. If an error condition isn't handled, put it on your to-do for later. No refactoring of code yet! <br /> <br /> <br /> <br /> Why is it important to separate out the coverage test writing from refactoring and not mix them? <br />
  • If can't find a decent inflection point, may need to use system boundaries. This requires writing smoke tests against the whole system as described by McConnell. <br />
  • <br /> <br />

Test Driven Development Test Driven Development Presentation Transcript

  • Test Driven Development Working with Legacy Code Guy Davis Pason Systems
  • Introduction TDD is a changed mindset for development  One of the cornerstones of the Agile movement  Rejection of the defined engineering approach  No detailed designs set in stone up front − Embraces change through refactoring − Result is more cohesive and less coupled code  Regression test bed and easier integration 
  • TDD Process Complex view 1. Add a little test 2. Run all tests and fail Simple view 3. Make a little change 1. Make a test 4. Run all the tests and succeed 2. Make it run 5. Refactor to remove duplication 3. Make it right
  • Introductory Exercise Given three integers  representing the length of the sides of a triangle return: 1 if equilateral  2 if isosceles  3 if scalene  Throw an exception if  not well formed.
  • Writing tests Chose a simple test first, use small steps...  When you have a list of tests, order them to get most  benefit up front. Forces you to define interfaces early on  One thing at a time; keep a to-do list  What to test:  Requirements, assumptions, boundary cases − Goal is to get 100% code coverage with tests −
  • Making tests run TDD book offers three main approaches:  Fake It − Triangulation − Obvious Implementation − Goal is to get a passing test ASAP  The longer a test is failing, the less confident you − are
  • Fake It Purpose is to get test running ASAP  Ignore coding conventions  Example: addition operation  Why?  Let's us take little steps... − Provides more refactoring confidence − Keeps work focused −
  • Triangulation How can we abstract conservatively with tests?  Example: addition operation  Safest way to abstract an operation  Allows for very small steps:  Why are small steps important? − Do you always want to take small steps? −
  • Obvious Implementation When small steps are too slow  Simply implement the solution you see  Faster, but...  Important to watch that bar isn't red for long or − often Harder: solving both “code that works” and “clean − code” at the same time Shouldn't keep getting test failures doing this −
  • Make it right Remove code duplication (including tests)  Meet coding standards  Refactor code  Reconcile differences − Isolate change − Move method − Extract method / Inline method − Extract interface −
  • Patterns Red Bar Patterns One step test  TDD Patterns Starter test  Isolated test  Explanation test  Test list  Learning test  Test first  Regression test  Assert first  Evident data 
  • Patterns Green Bar Patterns Testing Patterns Fake it  Child test  Triangulate  Obvious Mock object   Implementation Log string  One to many  Crash test dummy  Broken test  Clean check-in 
  • TDD of Legacy code 1. Identify change points 2. Find an inflection point 3. Cover the inflection point a) Break the dependencies b) Write tests 4. Make changes 5. Refactor the covered code
  • Change Points Where will changes need to be made in code?  Don't assume the first spot you find is the best  Approach resulting in fewest changes may be − good? What about approach using simplest design? − Better to work in area with existing unit tests − Debuggers are useful in this stage 
  • Inflection Point A narrow interface to a set of classes or functions  Any changes in classes behind an inflection point are  either detectable at the inflection point or inconsequential to the application. Inflection points determined by compile time and  run time dependencies and code behavior. What are some examples from the your project? 
  • Covering an Inflection Point Covering with unit tests (islands of test)  Legacy code: no tests means lots of dependencies  If class, try to create an instance to see links − If method, look at signature (and for globals) − Look at interactions with data and resources −
  • Breaking Dependencies Database:  Find the smallest slice of data needed to get − functionality to run; everything else null/empty. Class:  Mock object: just answers back constants − Self shunt: object communicates with test case − Method:  Build up structures manually, don't rely on − complex helper methods
  • Writing Covering Tests Goal is to determine the current behavior  Reverse of TDD; covering existing functions  Some tips  Test at the boundary values − Ensure “golden” or normal values are tested − Tests for handled error conditions − Correctness is just the behavior of system now  If lucky, can use old requirements docs :) 
  • Make changes Now that area has test coverage, make changes  Use the full TDD cycle for the changes  Run the tests often  Refactor as much as needed  Extract method (create test first) − Extract class refactoring − Many others... −
  • References Beck, Kent. Test-Driven Development: By Example. Addison-Wesley. 2003  Feathers. Michael. Working Effectively with Legacy Code. Object Mentor,  Inc. 2002. http://www.objectmentor.com/resources/articles/ WorkingEffectivelyWithLegacyCode.pdf Parrish, Alan et al. Ordering Test Cases to Maximize Early Testing.  University of Alabama. 2003. http://www.agilealliance.com/articles/articles/ ExtremeUnitTesting.pdf Ynchausti, Randy A. Integrating Unit Testing Into a Software  Development Team's Process. Monster Consulting. 2003. http:// www.agilealliance.com/articles/articles/IntegratingUnitTesting.pdf