Test Driven Development - Workshop

  • 386 views
Uploaded on

TDD is NOT an expert level practice, it is rather simple habit of writing the test first!

TDD is NOT an expert level practice, it is rather simple habit of writing the test first!

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
386
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
12
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Test Driven Development (TDD) Workshop! ! Anjana Somathilake! Director, Engineering and Architecture at Leapset! ! Proprietary & Confidential
  • 2. Coverage!
  • 3. Problem #1! In the beginning, your source code was perfect!! Then it all changed…!
  • 4. Problem #2! “Fix one thing, break another!”!
  • 5. Problem #3! You know how to build it, ! but you don’t know how to prove that it works!!
  • 6. Problem #4! What if I change this piece of code?!
  • 7. Problem #5! Change Happens !
  • 8. Problem #6! Developers get to know about the issues only after the QA testing.!
  • 9. Types of Testing
 ! •  •  •  •  •  •  •  •  •  •  •  •  •  •  Unit testing! Installation testing! Compatibility testing! Smoke and sanity testing! Regression testing! Acceptance testing! Functional testing! Destructive testing! Performance testing! Usability testing! Accessibility testing! Security testing! Integration testing! A/B testing!
  • 10. Scope! •  Introduction to TDD! •  Unit Testing! ! •  Refactoring!
  • 11. Introduction to Test Driven Development
 !
  • 12. TDD as a Habit! “TDD is NOT an expert level practice, it is rather simple habit of writing the test first!”!
  • 13. Traditional vs. TDD !
  • 14. What TDD is all about?! Testing?! Design!
  • 15. TDD Lifecycle
 ! Write a failing test Refactor Write the code to make this test pass
  • 16. TDD Process
 !
  • 17. TDD Lifecycle - Detailed
 ! Add a Test – Each new feature begins with writing a test. This test must initially fail since it is written before the feature has been coded.! ! Run All Tests – validate that the test-harnesses work and that the new test does not mistakenly pass without requiring any new code.! ! Write Development Code – That will cause the test to pass.! ! Run Tests – If all test cases pass the code meets all the tested requirements.! ! Clean Up Code – Refactor code to make production ready removing any dummy/stub code.! ! Repeat Process – Starting with another new test, the cycle is then repeated to push forward the functionality.!
  • 18. TDD Results! •  Testable Code •  Working Software •  Enforce Thinking Ahead •  Clean & Maintainable Code •  Increment Code Quality •  Faster Feedback •  Bring Back the Joy of Coding
  • 19. TDD in a nutshell! 1.  Write a test! 2.  Watch the test fail! 3.  Write just enough code ! 4.  Pass the test! 5.  Refactor, remove duplications! 6.  Pass the test again!
  • 20. TDD in extremely simplified
 ! •  Write a failing test! •  Pass the test! •  Repeat!
  • 21. Unit Testing
 !
  • 22. Unit Testing – Software Equivalent of Exercising !
  • 23. Unit Testing
 ! A unit test is a piece of code that invokes a unit of work in the system and then checks a single assumption about the behavior of that unit of work.!
  • 24. Unit of Work
 ! •  A unit of work is a single logical functional use case in the system that can be invoked by some public interface (in most cases)! •  A unit of work can span a single method, a whole class or multiple classes working together to achieve one single logical purpose that can be verified!
  • 25. Unit Test Frameworks! SUnit! Smalltalk! ! JUnit! Java! ! NUnit! .NET! ! OCUnit! Objective-c! ! (etc.)!
  • 26. JUnit Framework!
  • 27. Assertions! A confident and forceful statement of fact or belief.! ! “I assert that the earth is round”! ! But if the assertion is wrong, then there is something fundamentally flawed about the understanding.! ! This is the fundamental principle used in unit testing.!
  • 28. Assertions in coding! •  At this point in my code, I assert these two strings are equal! ! •  At this point in my code, I assert this object is not null!
  • 29. Assert in JUnit! assertEquals("failure - strings not same", 5l, 5l); assertNotNull("should not be null", new Object()); int objA[] = new int[10]; int objB[] = new int[10]; assertArrayEquals(objA, objB); https://github.com/junit-team/junit/wiki/Assertions
  • 30. Bank Account! Bank Account balance getBalance() deposit() withdraw()
  • 31. Bank Account! Sprint 1! ! •  •  •  •  Create a bank account! Deposit 100! Withdraw 50! Check the balance! ! ! Sprint 2 ! •  Max withdrawal is 5000! •  Add penalty of 5, if withdraw more than the balance! ! ! Sprint 3 •  Throw an exception if negative value is passed to the deposit method
  • 32. Unit Test Structure! Arrange Act Assert @Test public void deposit500(){ BankAccount bankAcct = new BankAccount(); bankAcct.deposit(500); assertEquals(500, bankAcct.getBalance()); }
  • 33. Unit Test Code Coverage! How much unit testing is enough?! ! How to measure the coverage?! ! Is there a tool for this?!
  • 34. Unit Test Code Coverage!
  • 35. Benefits of Unit Tests
 
 ! •  Fast! •  Easy to write and maintain! •  Better code coverage! •  When fail, provide insight into the problem!
  • 36. Refactoring!
  • 37. Refactoring! Refactoring is the process of clarifying and simplifying the design of existing code, without changing its behavior.! ! ! At the smallest scale, a “refactoring” is a minimal, safe transformation of your code that keeps its behavior unchanged.!
  • 38. Most well-known Refactoring Patterns.!
  • 39. Inline Temporary Variable
 ! Replace all uses of a local variable by inserting copies of its assigned value.! public double applyDiscount() { double basePrice = _cart.totalPrice(); return basePrice * discount(); } public double applyDiscount() { return _cart.totalPrice() * discount(); }
  • 40. Extract method! Create a new method by extracting a code fragment from an existing! method.! public void report(Writer out, List<Machine> machines) throws IOException { for (Machine machine : machines) { out.write(“Machine “ + machine.name()); if (machine.bin() != null) out.write(“ bin=” + machine.bin()); out.write(“n”); } } public void report(Writer out, List<Machine> machines) throws IOException { for (Machine machine : machines) { reportMachine(out, machine); } } private void reportMachine(Writer out, Machine machine) throws IOException { out.write(“Machine “ + machine.name()); if (machine.bin() != null){ out.write(“ bin=” + machine.bin()); } out.write(“n”); }
  • 41. String Calculator Exercise! 1.  Create a simple String calculator with a method int Add(string numbers)! 1.  The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it will return 0) for example “” or “1” or “1,2”! 2.  Start with the simplest test case of an empty string and move to 1 and two numbers! 3.  Remember to solve things as simply as possible so that you force yourself to write tests you did not think about! 4.  Remember to refactor after each passing test! 2.  Allow the Add method to handle an unknown amount of numbers! ! 3.  Calling Add with a negative number will throw an exception “negatives not allowed”!
  • 42. Thank You!!