Test Driven Development (TDD) Workshop!
!
Anjana Somathilake!
Director, Engineering and Architecture at Leapset!

!
Propri...
Coverage!
Problem #1!

In the beginning, your source code
was perfect!!
Then it all changed…!
Problem #2!

“Fix one thing, break another!”!
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!...
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...
TDD Results!

• 

Testable Code

• 

Working Software

• 

Enforce Thinking Ahead 

• 

Clean & Maintainable Code

• 

Inc...
TDD in a nutshell!

1.  Write a test!
2.  Watch the test fail!
3.  Write just enough code !
4.  Pass the test!
5.  Refacto...
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
assumpt...
Unit of Work

!

•  A unit of work is a single logical functional
use case in the system that can be invoked
by some publi...
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 asse...
Assertions in coding!

•  At this point in my code, I assert these two
strings are equal!
!
•  At this point in my code, I...
Assert in JUnit!
assertEquals("failure - strings not same", 5l, 5l);
assertNotNull("should not be null", new Object());

i...
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
...
Unit Test Structure!

Arrange

Act

Assert

@Test
public void deposit500(){
BankAccount bankAcct = new BankAccount();
bank...
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...
Refactoring!
Refactoring!

Refactoring is the process of clarifying and
simplifying the design of existing code,
without changing its b...
Most well-known Refactoring Patterns.!
Inline Temporary Variable

!
Replace all uses of a local variable by inserting copies of its assigned value.!

public doub...
Extract method!
Create a new method by extracting a code fragment from an existing!
method.!
public void report(Writer out...
String Calculator Exercise!

1.  Create a simple String calculator with a method int Add(string
numbers)!
1.  The method c...
Thank You!!
Test Driven Development - Workshop
Test Driven Development - Workshop
Upcoming SlideShare
Loading in...5
×

Test Driven Development - Workshop

459

Published on

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

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
459
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
15
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Test Driven Development - Workshop

  1. 1. Test Driven Development (TDD) Workshop! ! Anjana Somathilake! Director, Engineering and Architecture at Leapset! ! Proprietary & Confidential
  2. 2. Coverage!
  3. 3. Problem #1! In the beginning, your source code was perfect!! Then it all changed…!
  4. 4. Problem #2! “Fix one thing, break another!”!
  5. 5. Problem #3! You know how to build it, ! but you don’t know how to prove that it works!!
  6. 6. Problem #4! What if I change this piece of code?!
  7. 7. Problem #5! Change Happens !
  8. 8. Problem #6! Developers get to know about the issues only after the QA testing.!
  9. 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. 10. Scope! •  Introduction to TDD! •  Unit Testing! ! •  Refactoring!
  11. 11. Introduction to Test Driven Development
 !
  12. 12. TDD as a Habit! “TDD is NOT an expert level practice, it is rather simple habit of writing the test first!”!
  13. 13. Traditional vs. TDD !
  14. 14. What TDD is all about?! Testing?! Design!
  15. 15. TDD Lifecycle
 ! Write a failing test Refactor Write the code to make this test pass
  16. 16. TDD Process
 !
  17. 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. 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. 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. 20. TDD in extremely simplified
 ! •  Write a failing test! •  Pass the test! •  Repeat!
  21. 21. Unit Testing
 !
  22. 22. Unit Testing – Software Equivalent of Exercising !
  23. 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. 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. 25. Unit Test Frameworks! SUnit! Smalltalk! ! JUnit! Java! ! NUnit! .NET! ! OCUnit! Objective-c! ! (etc.)!
  26. 26. JUnit Framework!
  27. 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. 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. 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. 30. Bank Account! Bank Account balance getBalance() deposit() withdraw()
  31. 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. 32. Unit Test Structure! Arrange Act Assert @Test public void deposit500(){ BankAccount bankAcct = new BankAccount(); bankAcct.deposit(500); assertEquals(500, bankAcct.getBalance()); }
  33. 33. Unit Test Code Coverage! How much unit testing is enough?! ! How to measure the coverage?! ! Is there a tool for this?!
  34. 34. Unit Test Code Coverage!
  35. 35. Benefits of Unit Tests
 
 ! •  Fast! •  Easy to write and maintain! •  Better code coverage! •  When fail, provide insight into the problem!
  36. 36. Refactoring!
  37. 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. 38. Most well-known Refactoring Patterns.!
  39. 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. 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. 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. 42. Thank You!!
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×