TDD – Test Driven Development David Paluy June 15, 2008
Agenda <ul><li>WHY </li></ul><ul><li>HOW </li></ul><ul><li>+ Example </li></ul>
T est  D riven  D evelopment <ul><li>You  don’t have to do TDD ,  </li></ul><ul><li>You  should do TDD . </li></ul>
When do we need to write tests? If my code works perfectly, why should I write tests for it?
Realization 1: <ul><li>Testing breeds  confidence </li></ul>
Confidence <ul><li>Modular code </li></ul><ul><li>Many changes </li></ul><ul><li>Refactoring </li></ul><ul><li>NOTE:  If t...
When do we need to write tests? So, if the code works perfectly, and I’m never going to change it, then why should I write...
Realization 2: <ul><li>If you write tests before you write your code, you end up writing  better code . </li></ul>
If I write my  test first : <ul><li>It forces me to think before I code </li></ul><ul><li>Code is more focused and clear <...
The TDD process <ul><li>Write specifications </li></ul><ul><li>Write tests </li></ul><ul><li>Write code </li></ul>
The TDD process RED GREEN REFACTOR
The TDD process Write the tests Write code until tests pass Try to optimize the code
When do we need to write tests? So, if the code works perfectly, and I’m never going to change it, and the design is alrea...
Realization 3: <ul><li>Well written tests are just as good, if not  better than documentation . </li></ul><ul><li>NOTE:  T...
Yoga Instructions <ul><li>Kneel down </li></ul><ul><li>Put your hands flat in the ground </li></ul><ul><li>Lift your feet ...
Yoga Instructions
Yoga Instructions Examples > Comments
When do we need to write tests? So, if the code works perfectly, and I’m never going to change it, and the design is alrea...
The moral
Learning  T est  D riven  D evelopment
The Goal
Software Test Hierarchy <ul><li>  Black Box White Box </li></ul>FIT  –  F ramework for  I ntegrated  T ests nUnit   – C# j...
Example <ul><li>Need an action which:  submits a credit card </li></ul><ul><li>Specifications: </li></ul><ul><li>The user ...
Example: Credit Card Submition <ul><li>TEST(CREDIT_CARD_SUBMITION) </li></ul><ul><li>{ </li></ul><ul><li>MY_CREDIT_CARD = ...
Example: Charge Credit Card <ul><li>TEST(CHARGE_CREDIT_CARD) </li></ul><ul><li>{ </li></ul><ul><li>MY_CREDIT_CARD = 1234 5...
Example: Charge Credit Card <ul><li>TEST(CHARGE_CREDIT_CARD) </li></ul><ul><li>{ </li></ul><ul><li>MY_CREDIT_CARD = 1234 5...
T est  D riven  D evelopment <ul><li>Inspect </li></ul><ul><li>+ </li></ul><ul><li>Adapt </li></ul>
T est  D riven  D evelopment <ul><li>Apply </li></ul><ul><li>Inspect </li></ul><ul><li>Adapt </li></ul>
T est  D riven  D evelopment <ul><li>Apply </li></ul><ul><li>Inspect </li></ul><ul><li>Adapt </li></ul><ul><li>If you have...
Upcoming SlideShare
Loading in …5
×

Test driven-development

1,085 views
987 views

Published on

Test Driven Development
Why and How

Published in: Technology, Economy & Finance
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,085
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
7
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Test driven-development

  1. 1. TDD – Test Driven Development David Paluy June 15, 2008
  2. 2. Agenda <ul><li>WHY </li></ul><ul><li>HOW </li></ul><ul><li>+ Example </li></ul>
  3. 3. T est D riven D evelopment <ul><li>You don’t have to do TDD , </li></ul><ul><li>You should do TDD . </li></ul>
  4. 4. When do we need to write tests? If my code works perfectly, why should I write tests for it?
  5. 5. Realization 1: <ul><li>Testing breeds confidence </li></ul>
  6. 6. Confidence <ul><li>Modular code </li></ul><ul><li>Many changes </li></ul><ul><li>Refactoring </li></ul><ul><li>NOTE: If there are no tests, you should be worried making changes! </li></ul>
  7. 7. When do we need to write tests? So, if the code works perfectly, and I’m never going to change it, then why should I write tests?
  8. 8. Realization 2: <ul><li>If you write tests before you write your code, you end up writing better code . </li></ul>
  9. 9. If I write my test first : <ul><li>It forces me to think before I code </li></ul><ul><li>Code is more focused and clear </li></ul><ul><li>I know when I’m finished, because all the tests pass </li></ul>
  10. 10. The TDD process <ul><li>Write specifications </li></ul><ul><li>Write tests </li></ul><ul><li>Write code </li></ul>
  11. 11. The TDD process RED GREEN REFACTOR
  12. 12. The TDD process Write the tests Write code until tests pass Try to optimize the code
  13. 13. When do we need to write tests? So, if the code works perfectly, and I’m never going to change it, and the design is already flawless, then why should I write tests?
  14. 14. Realization 3: <ul><li>Well written tests are just as good, if not better than documentation . </li></ul><ul><li>NOTE: The real example is a better explanation than documents or comments. </li></ul>
  15. 15. Yoga Instructions <ul><li>Kneel down </li></ul><ul><li>Put your hands flat in the ground </li></ul><ul><li>Lift your feet off the ground and balance on your hands </li></ul><ul><li>Put your knees ahead of your hands </li></ul><ul><li>Stretch out your back so your body is perpendicular to the ground </li></ul>
  16. 16. Yoga Instructions
  17. 17. Yoga Instructions Examples > Comments
  18. 18. When do we need to write tests? So, if the code works perfectly, and I’m never going to change it, and the design is already flawless and it is totally readable and understandable, then why should I write tests?
  19. 19. The moral
  20. 20. Learning T est D riven D evelopment
  21. 21. The Goal
  22. 22. Software Test Hierarchy <ul><li> Black Box White Box </li></ul>FIT – F ramework for I ntegrated T ests nUnit – C# jUnit – Java bMock – C++ Unit Tests Integrated Tests Acceptance Tests Endurance Tests Stress Tests
  23. 23. Example <ul><li>Need an action which: submits a credit card </li></ul><ul><li>Specifications: </li></ul><ul><li>The user can submit his credit card </li></ul><ul><li>The server attempts to charge the credit card </li></ul><ul><li>On failure, display an error message </li></ul><ul><li>On failure, render a credit card form again </li></ul><ul><li>On success, store the transaction </li></ul><ul><li>On success, redirect to the receipt page </li></ul>
  24. 24. Example: Credit Card Submition <ul><li>TEST(CREDIT_CARD_SUBMITION) </li></ul><ul><li>{ </li></ul><ul><li>MY_CREDIT_CARD = 1234 5678 1234 5678; </li></ul><ul><li>TEST_NO_THROW (status = server.submitCreditCard(MY_CREDIT_CARD) ); </li></ul><ul><li>ASSERT.EQUAL (STATUS.SUBMITTED, status); </li></ul><ul><li>ASSERT.EQUAL (MY_CREDIT_CARD, server.getCreditCard()); </li></ul><ul><li>} </li></ul>
  25. 25. Example: Charge Credit Card <ul><li>TEST(CHARGE_CREDIT_CARD) </li></ul><ul><li>{ </li></ul><ul><li>MY_CREDIT_CARD = 1234 5678 1234 5678; </li></ul><ul><li>TEST_NO_THROW (status = server.chargeCreditCard(MY_CREDIT_CARD) ); </li></ul><ul><li>ASSERT.EQUAL (STATUS.CHARGED, status); </li></ul><ul><li>} </li></ul>
  26. 26. Example: Charge Credit Card <ul><li>TEST(CHARGE_CREDIT_CARD) </li></ul><ul><li>{ </li></ul><ul><li>MY_CREDIT_CARD = 1234 5678 1234 5678; </li></ul><ul><li>RECORD_MOCK(status = STATUS.CHARGED, server.chargeCreditCard(MY_CREDIT_CARD)); </li></ul><ul><li>TEST_NO_THROW(status = server.chargeCreditCard(MY_CREDIT_CARD) ); </li></ul><ul><li>ASSERT.EQUAL(STATUS.CHARGED, status); </li></ul><ul><li>} </li></ul>
  27. 27. T est D riven D evelopment <ul><li>Inspect </li></ul><ul><li>+ </li></ul><ul><li>Adapt </li></ul>
  28. 28. T est D riven D evelopment <ul><li>Apply </li></ul><ul><li>Inspect </li></ul><ul><li>Adapt </li></ul>
  29. 29. T est D riven D evelopment <ul><li>Apply </li></ul><ul><li>Inspect </li></ul><ul><li>Adapt </li></ul><ul><li>If you haven’t done it first, you don’t know what you are talking about! </li></ul>

×