unit testing

19
Unit Testing By Mohamad Asmar a.k.a DaiGooR

Upload: mohammad-asmar

Post on 04-Jul-2015

125 views

Category:

Technology


0 download

DESCRIPTION

session about unit testing at Najah National University .. for Nablus tech meeting NTM

TRANSCRIPT

Page 1: Unit testing

Unit TestingBy Mohamad Asmar

a.k.a DaiGooR

Page 2: Unit testing

Me!

Mohamed Asmer

Developer

[email protected]

www.daigoor.com

@daigoor

(+970) 598 917 280

Page 3: Unit testing

Developer life !

the more pressure you feel, the fewer tests

you writes.

The fewer tests you write, the less

productive you are and the less stable your

code becomes.

The less productive and accurate you are,

the more pressure you feel.

Page 4: Unit testing

Def.

Wikipedia : unit testing is a software

testing method by which individual units

of source code

In a simple way … validate each and

every unit of the software perform as

designed.

Page 5: Unit testing

Def. Cont.

A Unit is the smallest part of code›{function or procedure}

›The smallest part that can be compiled by it

self.

Page 6: Unit testing

Ex

public int stringToInt( String str ){

return Integer.parseInt(str);

}

stringToInt(null);

stringToInt(“TEXT”)

stringToInt(“1234567890987654321”)

Page 7: Unit testing

Why ?

Without it defects will appear at the end

of the cycle!

Trace bugs is time consuming , hard and

complex

Check new feature if they are feasible

Check input and output values

Correct outputs fast

Page 8: Unit testing

Why ? -_-

Reduce future cost !

Faster development

Better design

Faster debugging

Excellent regression tool

+

it provide sort of documentation

Page 9: Unit testing
Page 10: Unit testing

Categories

White-Box : function exe and checked

Black-Box : input/output & app-interface

Page 11: Unit testing

Types

Positive test (end user testing)›Valid parameter

Negative test ›Not valid parameter

Page 12: Unit testing

Ex

public int stringToInt( String str ){

return Integer.parseInt(str);

}

stringToInt(null)//N

stringToInt(“TEXT”)//N

stringToInt(“1234567890987654321”)//N

stringToInt(“1234”)//P

Page 13: Unit testing

When to write the test?

“Whenever you are tempted to type

something into a print statement or a

debugger expression, write it as a test

instead.”...

Page 14: Unit testing

story

:$

Page 15: Unit testing

Traditional Testing Strategies

Print Statements

Use of Debugger

Debugger Expressions

Page 16: Unit testing

Ex.

... List<?> getVersionsForProductId(Long id){ … return List<?> }

The Tests That Are Needed

Starting from the easiest to the hardest:

If there’s no product for that id, then an exception is thrown.

If there’s no versions for a valid product id, then an empty list is

returned.

If there are versions for a product id, then a non-empty list of all the

versions is returned.

Page 17: Unit testing

Live Show

Eclipse

IntelliJ Idea

Page 18: Unit testing

Keep Unit tests small and fast We should run them after each checkin

keep them UN-dependent

They should not depend on each other

They should be simple

Fix error immediately

Page 19: Unit testing

Its all about unit There should be a test class per each class .. no

more no less ! You should not right an application

to test another application.

Start with the simple test

Code convention