Test Driven Development - Workshop
Upcoming SlideShare
Loading in...5

Test Driven Development - Workshop



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!



Total Views
Views on SlideShare
Embed Views



2 Embeds 14

http://www.linkedin.com 13
https://www.linkedin.com 1



Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
Post Comment
Edit your comment

Test Driven Development - Workshop Test Driven Development - Workshop Presentation Transcript

  • Test Driven Development (TDD) Workshop! ! Anjana Somathilake! Director, Engineering and Architecture at Leapset! ! Proprietary & Confidential
  • Coverage!
  • Problem #1! In the beginning, your source code was perfect!! Then it all changed…! View slide
  • Problem #2! “Fix one thing, break another!”! View slide
  • Problem #3! You know how to build it, ! but you don’t know how to prove that it works!!
  • Problem #4! What if I change this piece of code?!
  • Problem #5! Change Happens !
  • Problem #6! Developers get to know about the issues only after the QA testing.!
  • 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!
  • Scope! •  Introduction to TDD! •  Unit Testing! ! •  Refactoring!
  • Introduction to Test Driven Development
  • TDD as a Habit! “TDD is NOT an expert level practice, it is rather simple habit of writing the test first!”!
  • Traditional vs. TDD !
  • What TDD is all about?! Testing?! Design!
  • TDD Lifecycle
 ! Write a failing test Refactor Write the code to make this test pass
  • TDD Process
  • 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.!
  • TDD Results! •  Testable Code •  Working Software •  Enforce Thinking Ahead •  Clean & Maintainable Code •  Increment Code Quality •  Faster Feedback •  Bring Back the Joy of Coding
  • 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!
  • TDD in extremely simplified
 ! •  Write a failing test! •  Pass the test! •  Repeat!
  • Unit Testing
  • Unit Testing – Software Equivalent of Exercising !
  • 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.!
  • 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!
  • Unit Test Frameworks! SUnit! Smalltalk! ! JUnit! Java! ! NUnit! .NET! ! OCUnit! Objective-c! ! (etc.)!
  • JUnit Framework!
  • 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.!
  • 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!
  • 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
  • Bank Account! Bank Account balance getBalance() deposit() withdraw()
  • 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
  • Unit Test Structure! Arrange Act Assert @Test public void deposit500(){ BankAccount bankAcct = new BankAccount(); bankAcct.deposit(500); assertEquals(500, bankAcct.getBalance()); }
  • Unit Test Code Coverage! How much unit testing is enough?! ! How to measure the coverage?! ! Is there a tool for this?!
  • Unit Test Code Coverage!
  • Benefits of Unit Tests
 ! •  Fast! •  Easy to write and maintain! •  Better code coverage! •  When fail, provide insight into the problem!
  • Refactoring!
  • 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.!
  • Most well-known Refactoring Patterns.!
  • 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(); }
  • 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”); }
  • 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”!
  • Thank You!!