Test Driven Development - Workshop

44
Proprietary & Confidential Test Driven Development (TDD) Workshop Anjana Somathilake Director, Engineering and Architecture at Leapset

description

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

Transcript of Test Driven Development - Workshop

Page 1: Test Driven Development - Workshop

Proprietary & Confidential

Test Driven Development (TDD) Workshop!!

Anjana Somathilake!Director, Engineering and Architecture at Leapset!

!

Page 2: Test Driven Development - Workshop

Coverage!

Page 3: Test Driven Development - Workshop
Page 4: Test Driven Development - Workshop

Problem #1!

In the beginning, your source code was perfect!!

Then it all changed…!

Page 5: Test Driven Development - Workshop

Problem #2!

“Fix one thing, break another!”!

Page 6: Test Driven Development - Workshop

Problem #3!

You know how to build it, !but you don’t know how to prove

that it works!!

Page 7: Test Driven Development - Workshop

Problem #4!

What if I change this piece of code?!

Page 8: Test Driven Development - Workshop

Problem #5!

Change Happens !

Page 9: Test Driven Development - Workshop

Problem #6!

Developers get to know about the issues only after the QA testing.!

Page 10: Test Driven Development - Workshop

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!

Page 11: Test Driven Development - Workshop

Scope!

•  Introduction to TDD!

•  Unit Testing!

•  Refactoring!!

Page 12: Test Driven Development - Workshop

Introduction to Test Driven Development!

Page 13: Test Driven Development - Workshop

TDD as a Habit!

“TDD is NOT an expert level practice, it is rather simple habit

of writing the test first!”!

Page 14: Test Driven Development - Workshop

Traditional vs. TDD !

Page 15: Test Driven Development - Workshop

What TDD is all about?!

Testing?!

Design!

Page 16: Test Driven Development - Workshop
Page 17: Test Driven Development - Workshop

TDD Lifecycle !

Write a failing test

Write the code to

make this test pass

Refactor

Page 18: Test Driven Development - Workshop

TDD Process!

Page 19: Test Driven Development - Workshop

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.!

Page 20: Test Driven Development - Workshop

TDD Results!

•  Testable Code

•  Working Software

•  Enforce Thinking Ahead

•  Clean & Maintainable Code

•  Increment Code Quality

•  Faster Feedback

•  Bring Back the Joy of Coding

Page 21: Test Driven Development - Workshop

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!

Page 22: Test Driven Development - Workshop

TDD in extremely simplified !

•  Write a failing test!

•  Pass the test!

•  Repeat!

Page 23: Test Driven Development - Workshop

Unit Testing !

Page 24: Test Driven Development - Workshop

Unit Testing – Software Equivalent of Exercising !

Page 25: Test Driven Development - Workshop

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.!

Page 26: Test Driven Development - Workshop

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!

Page 27: Test Driven Development - Workshop

Unit Test Frameworks!

SUnit!Smalltalk!

!JUnit!Java!

!NUnit!.NET!

!OCUnit!

Objective-c!!

(etc.)!

Page 28: Test Driven Development - Workshop

JUnit Framework!

Page 29: Test Driven Development - Workshop

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.!

Page 30: Test Driven Development - Workshop

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!

Page 31: Test Driven Development - Workshop

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

Page 32: Test Driven Development - Workshop

Bank Account!

Bank Account

balance

getBalance() deposit() withdraw()

Page 33: Test Driven Development - Workshop

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

Page 34: Test Driven Development - Workshop

Unit Test Structure!

Arrange Act Assert

@Test public void deposit500(){ BankAccount bankAcct = new BankAccount(); bankAcct.deposit(500); assertEquals(500, bankAcct.getBalance()); }

Page 35: Test Driven Development - Workshop

Unit Test Code Coverage!

How much unit testing is enough?!!

How to measure the coverage?!!

Is there a tool for this?!

Page 36: Test Driven Development - Workshop

Unit Test Code Coverage!

Page 37: Test Driven Development - Workshop

Benefits of Unit Tests!

•  Fast!

•  Easy to write and maintain!

•  Better code coverage!

•  When fail, provide insight into the

problem!

Page 38: Test Driven Development - Workshop

Refactoring!

Page 39: Test Driven Development - Workshop

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.!

Page 40: Test Driven Development - Workshop

Most well-known Refactoring Patterns.!

Page 41: Test Driven Development - Workshop

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

public double applyDiscount() { return _cart.totalPrice() * discount();

}

public double applyDiscount() { double basePrice = _cart.totalPrice(); return basePrice * discount();

}

Page 42: Test Driven Development - Workshop

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) { 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”);

}

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”); }

}

Page 43: Test Driven Development - Workshop

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”!

Page 44: Test Driven Development - Workshop

Thank You!!