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.

Test Driven Development (TDD) Basics

80 views

Published on

Presentation about TDD on the GDCR 2016 event organized at the ThoughtWorks South Africa offices

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Test Driven Development (TDD) Basics

  1. 1. Test Driven Development
  2. 2. Test Driven Development  Technique to design and develop software.  Practice part of the Extreme Programming methodology — XP  Based on three fundamental pillars.
  3. 3. TheThree Pillars  Implement the exact functionality of the software.  Reduce the number of defects on the software – good quality software  Produce modular and highly reusable software.
  4. 4. When Doing TDD  It’s not about just writing tests.  Translate use cases to examples.  Enough to describe the functionality without ambiguity.  Architecture will eventually emerge.
  5. 5. TheTDD Algorithm Red GreenRefactor
  6. 6. TheTDD Algorithm Red GreenRefactor
  7. 7. TDD InAction With an example ;-)
  8. 8. The Problem Email validation mechanism
  9. 9. The Problem Email validation mechanism
  10. 10. The Problem Email validation mechanism Neither empty nor null. Has to contain an @ symbol Has to belong to the .com, .net and .eduTDLs Domain part can’t be 1 character long All lowercase Not contain numbers Only _ . - symbols
  11. 11. Before we begin WritingTests  Testing framework  Part of the language  Ruby  Rust  Go  Python  External tools and libraries TestNG MSTest
  12. 12. Neither empty nor null Red @Test public void shouldReturnFalseIfEmailIsNull() { String email = null; EmailValidator validator = new EmailValidator(); boolean isValid = validator.isValid(email); assertThat(isValid, equalTo(false)); }
  13. 13. Neither empty nor null Green public boolean isValid(String email) { return false; }
  14. 14. Neither empty nor null Red @Test public void shouldReturnTrueIfEmailLengthIsGreaterThanZero() { String email = "some@email.com"; EmailValidator validator = new EmailValidator(); boolean isValid = validator.isValid(email); assertThat(isValid, equalTo(true)); }
  15. 15. Neither empty nor null Green public boolean isValid(String email) { if (email == null) { return false; } return email.length() > 0; }
  16. 16. Contains an @ symbol Red @Test public void shouldReturnFalseIfEmailDoesntContainAtSymbol () { String email = "someemail.com"; EmailValidator validator = new EmailValidator(); boolean isValid = validator.isValid(email); assertThat(isValid, equalTo(false)); }
  17. 17. Neither empty nor null Green public boolean isValid(String email) { if (email == null) { return false; } if (!email.contains("@")) { return false; } return email.length() > 0; }
  18. 18. Refactor private EmailValidator validator; @Before public void setUp () { validator = new EmailValidator(); }
  19. 19. In the end…  Components will behave exactly as we want.  Tests will be the documentation of our system.  Tests will break if changes compromise the system.
  20. 20. Thank you! Carlos Andrés Oquendo coquendo@thoughtworks.com @andres19_05

×