Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to test_driven_development

2,108 views

Published on

This is the slides of my talk @ Taiwan Java User Group on 2011/07/09.

You can watch the video here (in Chinese):
http://vimeo.com/26538602

Published in: Technology
  • Be the first to comment

Introduction to test_driven_development

  1. 1. Introduction toTest-Driven Development Hao-Cheng Lee 本著作係依據創用 CC Attribution-ShareAlike 3.0 Unported 授權 條款進行授權。如欲瀏覽本授權條款之副本,請造訪 http: //creativecommons.org/licenses/by-sa/3.0/ ,或寄信至 Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA 。
  2. 2. About me● Hao-Cheng Lee● Java Engineer for 7+ years● Software Engineer @ Armorize● Interested in Java, Scala, TDD, CI● email: haocheng.lee@gmail.com● twitter: haocheng● Google+: 浩誠
  3. 3. Download Java 7 RC Now Project Coin Slides
  4. 4. Agenda● What is TDD?● Why use TDD?● How to do TDD?● Pros and Cons● How do I start?
  5. 5. TDD = TFD +Refactoring
  6. 6. Not aboutwriting tests...
  7. 7. TDD is aboutwriting better code
  8. 8. from http://ch.englishbaby.com/forum/LifeTalk/thread/441379
  9. 9. Test Myths
  10. 10. I have no time for testing
  11. 11. Technical Debt from http://www.freedebtadvice-uk.com/
  12. 12. My code isBUG-FREE!
  13. 13. from http://vincentshy.pixnet.net/blog/post/5397455
  14. 14. QA will do the testing
  15. 15. Black Box Testing from http://www.jasonser.com/marketing-black-box/
  16. 16. http://www.ocoee.org/Departments/HR/
  17. 17. Fasterfrom http://cllctr.com/view/c2fdb4d2625e109069c843ea1bb99e50
  18. 18. from Test Driven Development Tutorial by Kirrily Robert
  19. 19. Faster● Shorter release cycle● Automation saves time● Find bugs earlier
  20. 20. Betterfrom http://www.nataliedee.com/archives/2005/sep/
  21. 21. Better● Greater code coverage● The courage to refactor● Prevent regression bugs● Improve your design
  22. 22. "Im not a great programmer;Im just a good programmerwith great habits." - Kent Beck
  23. 23. Testing the Old Way
  24. 24. TDD
  25. 25. TDD
  26. 26. How to do TDD?● Design: figure out what you want to do● Test: write a test to express the design ○ It should FAIL● Implement: write the code● Test again ○ It should PASS
  27. 27. DesignWe need a method add(), which takes two parameters and addthem together, then it will return the result.
  28. 28. Test
  29. 29. FAILjava.lang.AssertionError: expected:<2> but was:<0>...at tw.idv.haocheng.play.CalculatorTest.one_plus_one_is_two(CalculatorTest.java:20)
  30. 30. Implement
  31. 31. PASS
  32. 32. Write the least codeto make the test pass
  33. 33. More Test
  34. 34. FAILjava.lang.AssertionError: expected:<4> but was:<2>...at tw.idv.haocheng.play.CalculatorTest.two_plus_two_is_four(CalculatorTest.java:25)
  35. 35. Implement
  36. 36. PASS
  37. 37. DesignThe add() method only accept positive numbers
  38. 38. Test
  39. 39. FAILjava.lang.AssertionError: IllegalArgumentException expectedat org.junit.Assert.fail(Assert.java:91)at tw.idv.haocheng.play.CalculatorTest.negative_numbers_will_throw_exception(CalculatorTest.java:32)
  40. 40. Implement
  41. 41. PASS
  42. 42. from http://www.oracle.com/
  43. 43. DesignWe need a addNewCustomer()method that will save a customer record in Database
  44. 44. Test
  45. 45. Test
  46. 46. FAILjava.lang.NullPointerExceptionat tw.idv.haocheng.CustomerDAOTest.addNewCustomer(CustomerDAOTest.java:24)
  47. 47. Implement
  48. 48. PASS
  49. 49. from https://www.google.com/accounts/NewAccount
  50. 50. Design We hava a page that usercan apply for a new account
  51. 51. Test
  52. 52. Test
  53. 53. FAILwicket.markup.MarkupException: Unable to findcomponent with id accountFormPanel in[MarkupContainer ....at ngc.account.wicket.edit.CreateAccountPageTest.setUp(CreateAccountPageTest.java:49)
  54. 54. Implement
  55. 55. PASS
  56. 56. Unit Test Frameworks● Java - JUnit● Python - PyUnit● PHP - PHPUnit● Ruby - Test:Unit● Javascript - Jasmine● .Net - NUnit
  57. 57. Theres No Silver Bulletfrom http://www.penn-olson.com/2009/12/22/social-media-the-silver-bullet/
  58. 58. Costsfrom http://ora-pl-sql.blogspot.com/2011/05/database-costs.html
  59. 59. It takes time...from http://chunkeat626.blogspot.com/2010_11_01_archive.html
  60. 60. Need tomaintain tests
  61. 61. TDD is not suitable for... from http://www.flickr.com/photos/ilike/2443295369/
  62. 62. from http://tw.gamelet.com/game.do?code=heroes
  63. 63. from http://dilbert.com/
  64. 64. But how do I start??from http://zekjevets.blogspot.com/2010/02/alternative-racism.html
  65. 65. from http://www.alexbolboaca.ro/wordpress/the-remote-pair-programming-tour
  66. 66. Suggestions● Prepare tools and environments● Learn to write Unit test● Start from easy ones
  67. 67. Reading List● The Bowling Game Kata● Unit Testing Guidelines● Why are we embarrassed to admit that we don’t know how to write tests? (中譯版本)● "The Clean Code Talks -- Unit Testing"● Top 10 things which make your code hard to test
  68. 68. References - Test-Driven Development● Test Driven Development Tutorial by Kirrily Robert● Engineer Notebook: An Extreme Programming Episode by Robert C. Martin and Robert S. Koss● Technical Debt by Martin Fowler● InfoQ: Testing Misconceptions by Liam OConnor● Unit Test Isolation● Erratic Test● Singletons are Evil● RSpec 讓你愛上寫測試

×