Introduction to TDD (Test Driven development) - Ahmed Shreef


Published on

A short 20 minute introduction to Test Driven Development with simple example on unit testing using JUnit

Published in: Technology
1 Like
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Introduction to TDD (Test Driven development) - Ahmed Shreef

  1. 1. Intro. To TDD  by  Ahmed Shreef       20 Minute talks
  2. 2. First, What is Unit Testing ?
  3. 3. A Unit is the smallest testable part of your application.
  4. 4. Unit Testing is all about breaking your application into small units and writing automated tests for each unit.
  5. 5. Benefits of Unit Testing ..
  6. 6. - Find problems early. - Reduce bug fixing time. - Monitoring of your code base.
  7. 7. Gives confidence when future code changes are required Real low-level regression tests
  8. 8. Real documentation that never lie
  9. 9. Back to TDD *Test Driven Development*
  10. 10. What we are used to do .. Design Code Test
  11. 11. The TDD way .. Design Test Refactor Test Code
  12. 12. Benefits of TDD .. The benefits of unit testing + …
  13. 13. Encourage thinking about the interface before the implementation So, Can lead to more modularized, flexible, and extensible code
  14. 14. Let Developers focus on the task at hand
  15. 15. %99~ Code coverage
  16. 16. Let me tell you a story ..
  17. 17. Boss told me that he wants to build a robot that can get our developers drinks and meals to save them the time they lose every day on these little things and let them focus on writing more code
  18. 18. I started by thinking about designing a Class that can warp the tasks done by the robot, and thought that we can name it OfficeBoy. It will have 2 methods : getMeal( mealName ) getDrink( drinkName )
  19. 19. My second step: write test for the first method .. getMeal()
  20. 20. import org.junit.*; import static org.junit.Assert; public class OfficeBoyTestCase { @Test Public void canGetMeals(){ OfficeBoy mabrouk = new OfficeBoy(); assertTrue( mabrouk.getMeal(“pizza”) instanceof Pizza ); } }
  21. 21. If we try to build and run this test it will Fail even to compile as we still don't have the required classes ( OffficeBoy and Pizza )
  22. 22. package com.shreef.robot; import com.shreef.robot.meals.*; public class OfficeBoy { public Meal getMeal(String mealName) { return new Pizza(); } }
  23. 23. package com.shreef.robot.meals; public interface Meal { } public class Pizza implements Meal { }
  24. 24. If we try to build and run this test it will Succeed, so now we are sure that our test works, and its time to continue the implementation of getMeal()
  25. 25. package com.shreef.robot; import com.shreef.robot.meals.*; public class OfficeBoy { public Meal getMeal(String mealName) { try { Class mealClass = Class.forName( “com.shreef.meals.”+mealName); Constructor construct = mealClass.getConstructor( new Class[0] ); return (Meal)construct.newInstance(); } catch (Exception e) { return null; } } }
  26. 26. So again we run the tests and it may Fail or Succeed. This is how TDD works
  27. 27. 2 weeks later, a code change was requested. Our employees wanted the robot to get them a default meal when the requested meal isn't available instead of getting them nothing “null”. They agreed that the default meal can be “Koshary” as it's an always available meal in this city.
  28. 28. 2 weeks, for a developer is enough to make him forget his name not just the code he wrote, and I didn't have enough time to do this task. So, I did ask one my colleagues to do it. And she came after 30 minutes and said that she did it !!
  29. 29. Can you guess what She did ?
  30. 30. Yes, she did read the tests and this let her understand how the code I wrote works without needing to ask me. Then, she edited the code and ran the tests and Woot!! it Succeeds .
  31. 31. This was a glance about TDD. Now, The biggest challenge is to know how to write testable code. This can be a talk by itself ;)
  32. 32. Thanks ..            Ahmed Shreef email:    blog:    twitter: @shreef