32
20 Minute talks Intro. To TDD by  Ahmed Shreef     [email protected]     

Introduction to TDD (Test Driven development) - Ahmed Shreef

Embed Size (px)

DESCRIPTION

A short 20 minute introduction to Test Driven Development with simple example on unit testing using JUnit

Citation preview

Page 1: Introduction to TDD (Test Driven development) - Ahmed Shreef

20 Minute talks

Intro. To TDD 

by  Ahmed Shreef

    [email protected]     

Page 2: Introduction to TDD (Test Driven development) - Ahmed Shreef

First, What is Unit Testing ?

Page 3: Introduction to TDD (Test Driven development) - Ahmed Shreef

A Unit is the smallest testable part of your application.

Page 4: Introduction to TDD (Test Driven development) - Ahmed Shreef

Unit Testing is all about breaking your application into small units and writing automated tests for each unit.

Page 5: Introduction to TDD (Test Driven development) - Ahmed Shreef

Benefits of Unit Testing ..

Page 6: Introduction to TDD (Test Driven development) - Ahmed Shreef

- Find problems early.

- Reduce bug fixing time.

- Monitoring of your code base.

Page 7: Introduction to TDD (Test Driven development) - Ahmed Shreef

Gives confidence when future code changes are required

Real low-level regression tests

Page 8: Introduction to TDD (Test Driven development) - Ahmed Shreef

Real documentation that never lie

Page 9: Introduction to TDD (Test Driven development) - Ahmed Shreef

Back to TDD

*Test Driven Development*

Page 10: Introduction to TDD (Test Driven development) - Ahmed Shreef

What we are used to do ..

Design

Test

Code

Page 11: Introduction to TDD (Test Driven development) - Ahmed Shreef

The TDD way ..

Design

Test Code

Test

Refactor

Page 12: Introduction to TDD (Test Driven development) - Ahmed Shreef

Benefits of TDD ..

The benefits of unit testing + …

Page 13: Introduction to TDD (Test Driven development) - Ahmed Shreef

Encourage thinking about the interface before the implementation

So, Can lead to more modularized, flexible, and extensible code

Page 14: Introduction to TDD (Test Driven development) - Ahmed Shreef

Let Developers focus on the task at hand

Page 15: Introduction to TDD (Test Driven development) - Ahmed Shreef

%99~ Code coverage

Page 16: Introduction to TDD (Test Driven development) - Ahmed Shreef

Let me tell you a story ..

Page 17: Introduction to TDD (Test Driven development) - Ahmed Shreef

Boss told me that he wants to build a robotthat can get our developers drinks and meals to save them the time they lose every day on these little things and let them focus on writing more code

Page 18: Introduction to TDD (Test Driven development) - Ahmed Shreef

I started by thinking about designing a Class thatcan warp the tasks done by the robot, and thought that we can name it OfficeBoy.

It will have 2 methods : getMeal( mealName ) getDrink( drinkName )

Page 19: Introduction to TDD (Test Driven development) - Ahmed Shreef

My second step:

write test for the first method .. getMeal()

Page 20: Introduction to TDD (Test Driven development) - Ahmed Shreef

import org.junit.*;import static org.junit.Assert;public class OfficeBoyTestCase {

@TestPublic void canGetMeals(){

OfficeBoy mabrouk = new OfficeBoy();

assertTrue( mabrouk.getMeal(“pizza”) instanceof Pizza );}

}

Page 21: Introduction to TDD (Test Driven development) - Ahmed Shreef

If we try to build and run this test it will Fail even to compile as we still don't have the required classes ( OffficeBoy and Pizza )

Page 22: Introduction to TDD (Test Driven development) - Ahmed Shreef

package com.shreef.robot;import com.shreef.robot.meals.*;

public class OfficeBoy {

public Meal getMeal(String mealName) {

return new Pizza(); }

}

Page 23: Introduction to TDD (Test Driven development) - Ahmed Shreef

package com.shreef.robot.meals;

public interface Meal { }

public class Pizza implements Meal { }

Page 24: Introduction to TDD (Test Driven development) - Ahmed Shreef

If we try to build and run this test it will Succeed,so now we are sure that our test works, and its time to continue the implementation of getMeal()

Page 25: Introduction to TDD (Test Driven development) - Ahmed Shreef

package com.shreef.robot;import com.shreef.robot.meals.*; public class OfficeBoy { public Meal getMeal(String mealName) {

try { Class mealClass = Class.forName( “com.shreef.meals.”+mealName); Constructor construct = mealClass.getConstructor( new Class[0] ); return (Meal)construct.newInstance(); } catch (Exception e) { return null; } }}

Page 26: Introduction to TDD (Test Driven development) - Ahmed Shreef

So again we run the tests and it may Fail or Succeed.

This is how TDD works

Page 27: Introduction to TDD (Test Driven development) - Ahmed Shreef

2 weeks later, a code change was requested.Our employees wanted the robot to get them a default meal when the requested meal isn't available instead of getting them nothing “null”.

They agreed that the default meal can be “Koshary”as it's an always available meal in this city.

Page 28: Introduction to TDD (Test Driven development) - Ahmed Shreef

2 weeks, for a developer is enough to make himforget his name not just the code he wrote, and I didn't have enough time to do this task.

So, I did ask one my colleagues to do it. And shecame after 30 minutes and said that she did it !!

Page 29: Introduction to TDD (Test Driven development) - Ahmed Shreef

Can you guess what She did ?

Page 30: Introduction to TDD (Test Driven development) - Ahmed Shreef

Yes, she did read the tests and this let her understand how the code I wrote works withoutneeding to ask me.

Then, she edited the code and ran the tests and Woot!! it Succeeds .

Page 31: Introduction to TDD (Test Driven development) - Ahmed Shreef

This was a glance about TDD. Now, The biggest challenge is to know how to write testable code.

This can be a talk by itself ;)

Page 32: Introduction to TDD (Test Driven development) - Ahmed Shreef

           Ahmed Shreef

email: [email protected]  

blog: http://shreef.com  

twitter: @shreef

Thanks ..