Introduction to TDD (Test Driven development) - Ahmed Shreef

Post on 08-Jul-2015

2.451 views 0 download

Tags:

description

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

Transcript of Introduction to TDD (Test Driven development) - Ahmed Shreef

20 Minute talks

Intro. To TDD 

by  Ahmed Shreef

    ahmed@shreef.com     

First, What is Unit Testing ?

A Unit is the smallest testable part of your application.

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

Benefits of Unit Testing ..

- Find problems early.

- Reduce bug fixing time.

- Monitoring of your code base.

Gives confidence when future code changes are required

Real low-level regression tests

Real documentation that never lie

Back to TDD

*Test Driven Development*

What we are used to do ..

Design

Test

Code

The TDD way ..

Design

Test Code

Test

Refactor

Benefits of TDD ..

The benefits of unit testing + …

Encourage thinking about the interface before the implementation

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

Let Developers focus on the task at hand

%99~ Code coverage

Let me tell you a story ..

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

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 )

My second step:

write test for the first method .. getMeal()

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

@TestPublic void canGetMeals(){

OfficeBoy mabrouk = new OfficeBoy();

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

}

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 )

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

public class OfficeBoy {

public Meal getMeal(String mealName) {

return new Pizza(); }

}

package com.shreef.robot.meals;

public interface Meal { }

public class Pizza implements Meal { }

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()

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

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

This is how TDD works

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.

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

Can you guess what She did ?

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 .

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

           Ahmed Shreef

email: ahmed@shreef.com  

blog: http://shreef.com  

twitter: @shreef

Thanks ..