0% found this document useful (0 votes)
60 views26 pages

Six Simple Steps To Unit Testing Happiness

A powerpoint presentation to teach you how to efectively create and maintain Unit Tests.

Uploaded by

eduardoDiaz98
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views26 pages

Six Simple Steps To Unit Testing Happiness

A powerpoint presentation to teach you how to efectively create and maintain Unit Tests.

Uploaded by

eduardoDiaz98
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Six Simple Steps to Unit Testing Happiness

Steven Feuerstein
[email protected] PL/SQL Evangelist, Quest Software www.oracleplsqlprogramming.com www.unit-test.com www.quest.com

Copyright 2006 Steven

Writing software is.....

Copyright 2006 Steven

Testing software is.....

Copyright 2006 Steven

Buggy software is....

Embarrassing Expensive Deadly


Copyright 2006 Steven

Buggy software is embarrassing


There can be as many as 20 to 30 bugs per 1,000 lines of software code !"ustainable #omputing #onsortium 32$ of organi%ations say that they release software with too many defects !#utter #onsortium 3&$ of organi%ations believe they lac' an ade(uate software (uality assurance program !#utter #onsortium 2)$ of organi%ations do not conduct any formal (uality reviews !#utter #onsortium Developers spend about &0$ of development costs on identifying and correcting defects !The *ational +nstitute of "tandards and Technology
Copyright 2006 Steven

Buggy software is expensive $60B per year in US alone! JUNE 25, 2002 (COMPUTERWORLD) WASHINGTON -- Softwa ! "#$% a ! &o%t'($ t)! U*S* !&o(o+, a( !%t'+at!- .5/*5 "'00'o( !a&) ,!a * Of t)! tota0 .5/*5 "'00'o( &o%t, #%! % '(&# !- 123 of t)! &o%t a(- -!4!0o5! % 613* T)! ! a ! 4! , f!w +a 7!t% w)! ! 8"#,! % a ! w'00'($ to a&&!5t 5 o-#&t% t)at t)!, 7(ow a ! $o'($ to +a0f#(&t'o(,8 %a'- G !$o , Ta%%!,, t)! Nat'o(a0 I(%t't#t! of Sta(-a -% a(- T!&)(o0o$, %!('o !&o(o+'%t w)o )!a-!- t)! %t#-,* 89#t %oftwa ! '% at t)! !:t !+! !(- '( t! +% of ! o % o "#$% t)at a ! '( t)! t,5'&a0 5 o-#&t w)!( 't '% %o0-*8 O), ,!% a(- ;2<= .6009> .1009>
Copyright 2006 Steven

Buggy software is !ea!ly


2006 Softwa ! fa'0# ! &o(t '"#t!% to 5ow! o#ta$! a& o%% t)! No t)!a%t! ( U*S* a(- Ca(a-a, 7'00'($ 6 5!o50!* 200? @'4! Pa(a+a('a( &a(&! 5at'!(t% -'! fo00ow'($ o4! -o%!% of a-'at'o(, a+o#(t% of w)'&) w! ! -!t! +'(!", fa#0t, #%! of %oftwa !* 2000 C a%) of a Ma '(! Co 5% O%5 !, t'0t- oto a' & aft 5a t'a00, "0a+!- o( A%oftwa ! a(o+a0,8 7'00% fo# %o0-'! %* ?//B Ra-a t)at &o#0- )a4! 5 !4!(t!- <o !a( C!t & a%) (7'00'($ 225) )o""0!- ", %oftwa ! 5 o"0!+* ?//5 A+! '&a( A' 0'(!% C!t, -!%&!(-'($ '(to Ca0', Co0o+"'a, & a%)!% '(to a +o#(ta'(, 7'00'($ ?5/* J# , )o0-% +a7! of f0'$)t-+a(a$!+!(t %,%t!+ ?B3 !%5o(%'"0!* A !5o t ", t)! U('4! %'t, of 9'!0!f!0- '( G! +a(, fo#(- t)at t)! %oftwa ! 5 !%!(t!- '(%#ff'&'!(t a(- &o(f0'&t'($ '(fo +at'o( to t)! 5'0ot%, w)o $ot 0o%t* Copyright 2006 Steven

"ow !o we avoi! buggy software #lear and accurate re(uirements #areful design Excellent tools ,est practices, standards, guidelines -that is, follow them. #ode review U) o)*** Thorough testing t)! wo 0- '% '(
"'$ t o#"0!* Copyright 2006 Steven

Woul!n#t it be great if... +t was easy to construct tests


An agree -upon an effective approac! to test construction t!at ever"one can un erstan an follow

+t was easy to run tests


An see t!e results, instantl" an automaticall".

Testing were completely integrated into my development, /0, and maintenance processes
#o program goes to QA until it !as passe a $atter" of tests An"one can maintain wit! confi ence, $ecause m" test suite automaticall" vali ates m" c!anges Copyright 2006 Steven

$ifferent types of testing


There are many types of testing1 functional2system tests, stress tests, unit tests 0 3unit test3 is the test of a single unit of code
Also %nown as a programmer test

4nit tests are the responsibility of developers 5 that is, us, the people in this room
#ot fun amentall" a &o$ for t!e QA epartment, w!ic! generall" focuses on functional an s"stem tests.

Copyright 2006 Steven

Trut% or $are 6ow do you -or your team. unit test your 782"/8 code today9

> 'e use automate testing software. > 'e !ave a formal test process t!at we eac! > >
follow, $ut ot!erwise a manual process. Ever"one oes t!eir own t!ing an we !ope for t!e $est. (ur users test our co e.

Copyright 2006 Steven

Unit testing reality 8et:s face it1 we 782"/8 developers don:t spend nearly enough time unit testing our code
For t!e most part, we run a script t!at ispla"s output on t!e screen an t!en we stare at all until we eci e if t!e test succee e or faile .

There are some understandable reasons1


)er" few tools an utilities !ave $een availa$le, to date, for PL/SQL testing. *anagers on+t give us enoug! time to prepare an e,ecute tests.
Copyright 2006 Steven

&roblems wit% Typi'al Testing

0lmost entirely ad hoc


#o compre!ensive effort to compile test cases #o infrastructure to recor cases an a minister tests

Difficult to verify correctness


#on-automate verification is slow an error-prone.

;elies on the user community to test


Since we are never reall" sure we-ve teste properl", we rel" on our users .or, we are luc%", t!e QA epartment/ to finis! our &o$
Copyright 2006 Steven

Typi'al Testing D,<"=>4T74T 74T=8+*E 5 unit testing mechanism of choice9


BEGIN DBMS_OUTPUT.PUT_LINE DBMS_OUTPUT.PUT_LINE DBMS_OUTPUT.PUT_LINE DBMS_OUTPUT.PUT_LINE DBMS_OUTPUT.PUT_LINE DBMS_OUTPUT.PUT_LINE DBMS_OUTPUT.PUT_LINE END;
"!tw(%t *%f "!tw(%t *t%t

(betwnstr (betwnstr (betwnstr (betwnstr (betwnstr (betwnstr (betwnstr

(NULL, 3, 5, (' b!"e#$%', (' b!"e#$%', (' b!"e#$%', (' b!"e#$%', (' b!"e#$%', (' b!"e#$%',

true)); &, 5, true)); 3, 5, true)); '3, '5, true)); NULL, 5, true)); 3, NULL, true)); 3, (&&, true));

There has got to be a better way!

Copyright 2006 Steven

(oving towar!s a Better Way #hange from within1 your code will not test itself
0ou must accept t!e responsi$ilit" an t!en $e iscipline .sig!...t!at+s not fun at all/. 1ommit to testing an watc! t!e wa" "ou write "our co e c!ange.

#hange from without1 new possibilities are on the hori%on?


)tt5=DD#t50%E0*%o# &!fo $!*(!tD utPLSQL )tt5=DDwww*#('t-t!%t*&o+ Quest 1o e 2ester for (racle

0h, but what about those six, simple steps9


Copyright 2006 Steven

Six Simple Steps to Unit Testing "appiness


1 Describe fully the re(uired functionality of the program 2 Define just the header of the program -name, parameter list, return value. 3 Elaborate the test cases for the program @ ,uild test code that implements all test cases A Brite the program unit C Test, debug, fix, test, debug, fix, test, debug Then repeat steps 35C for each enhancement and bug report
Copyright 2006 Steven

$es'ribe re)uire! fun'tionality


+ need a variation of "4,"T; that will return the portion of a string between specified start and end locations "ome specific re(uirements1 3t s!oul wor% li%e S45S26 as muc! as ma%es sense .treat a start location of 7 as 8, for e,ample9 if t!e en location is past t!e en of t!e string, t!e treat it as t!e en of t!e string/. #egative start an en s!oul return a su$string at t!e end of t!e string. Allow t!e user to specif" w!et!er or not t!e en points s!oul $e inclu e .
Copyright 2006 Steven

$efine t%e program spe'ifi'ation


)UN*TION betwnstr ( str+n$_+n IN ,-.*/-.0 , st rt_+n IN PLS_INTEGE. , en"_+n IN PLS_INTEGE. , +n!1us+2e_+n IN BOOLE-N DE)-ULT T.UE ) .ETU.N ,-.*/-.0 DETE.MINISTI*

<y specification or header should be compatible with all re(uirements


3 also self- ocument t!at t!e function is eterministic: no si e effects.

+ can -and will. now create a compile5able stub for the program Bhy do that9
5ecause 3 can t!en full" efine an implement m" test co e;
Copyright 2006 Steven
betwnstr0 sf

TNT o TDD>

*laborate t%e test 'ases

Before I write any code, + will come up with as many of the test cases as possible 55 and write my test code
2!is is %nown as <test- riven evelopment<. 2== is a ver" !ot topic among evelopers an is associate wit! Agile Software .!ttp://agilemanifesto.org// an E,treme Programming.

7utting aside the fancy names and methodologies, TDD ma'es perfect sense 55 when you stop to thin' about it
If ,o# w 't! ,o# 5 o$ a+ "!fo ! ,o# -!f'(! ,o# t!%t%, )ow -o ,o# 7(ow ,o# w)!( ,o#F ! -o(!> A(- 'f ,o# w 't! ,o# t!%t% afterward, ,o# a ! 0'7!0, to 5 !C#-'&! ,o# t!%t% to %)ow 8%#&&!%%*8 Copyright 2006 Steven

Some of t%e test 'ases for B*TW+ST,


"tart and end within the string -3normal3 usage. "tart of 0 End past end of string *ull string, string of single character, 32)C) len character *ull start and2or end *egative start and end "tart larger than end -positive and negative. Dariations of the above with different inclusive values

Do(Ft "! o4! w)!0+!- ", t)! tota0 (#+"! of &a%!%* Sta t o#t w't) a !5 !%!(tat'4! %a+50'($* ;o# &a( a0wa,% a-- f o+ t)! !* 9#t (ow a "'$ &)a00!($!= w)! ! -o ,o# 5#t t)'% t!%t &a%! '(fo +at'o(> T'! 't a% &0o%!0, a% 5o%%'"0! to ,o# &o-!*

Copyright 2006 Steven

Test'ases an! Test -o!e

The challenge -terror9. of the blan' screen


>ow o 3 efine t!e test cases? >ow o 3 set up t!ose tests? >ow o 3 verif" t!e results?

8et:s see how /uest #ode Tester for >racle helps me tac'le these challenges
=efine an maintain "our test cases t!roug! a grap!ical interface, t!en let it o all t!e wor%.
Copyright 2006 Steven

$on#t write test 'o!e . describe tests Time to enter the world of /uest #ode Tester for >racle
6at!er t!an write test co e, "ou escri$e t!e tests "ou nee t!roug! a grap!ical interface. 1o e 2ester saves "our escriptions in a test repositor". 3t generates a PL/SQL test pac%age $ase on "our escriptions. 3t runs t!e test at "our request an ispla"s t!e results.
Copyright 2006 Steven

Write t%e program. *ow that + 'now + can test the program, + can start implementing betwnstr Einally?
betwnstr1 sf

First version of 3between string3

*.E-TE O. .EPL-*E )UN*TION betwnstr ( str+n$_+n IN ,-.*/-.0 , st rt_+n IN PLS_INTEGE. , en"_+n IN PLS_INTEGE. , +n!1us+2e_+n IN BOOLE-N DE)-ULT T.UE ) .ETU.N ,-.*/-.0 DETE.MINISTI* IS BEGIN .ETU.N ( SUBST. ( str+n$_+n , st rt_+n , en"_+n ' st rt_+n 3 ( ) ); END;

Copyright 2006 Steven

Test/ !ebug/ fix/ test/ !ebug/ fix/ test/ !ebug... Bith a test script in place, + can (uic'ly and easily move bac' and forth between running my program, identifying errors, debugging and fixing the code, running the program again + also then have my test process and regression test in place so that as + ma'e enhancements or fix bugs, + can fall bac' on this foundation 3t is critical t!at "ou maintain "our test case efinitions an test co e as "our program evolves. An up ate t!ose first -- $efore "ou c!ange t!e program;
Copyright 2006 Steven

-%ange 0our Testing Ways /uest #ode Tester for >racle -and even ut78"/8. can ma'e a dramatic difference in your ability to test and your confidence in the resulting code ,uild a comprehensive 3library3 of unit tests as you build your application
2!ese tests an all t!eir test cases can $e passe on to ot!er evelopers An"one can now en!ance or maintain t!e co e wit! confi ence. *a%e "our c!anges an run t!e tests. 3f "ou get a green lig!t, "ou+re (@;
Copyright 2006 Steven

Testing1 Baby steps better t%an paralysis. 4nit testing is an intimidating process
0ou are never reall" one. 0ou !ave to maintain "our test co e along wit! "our application co e.

,ut every incremental improvement in testing yields immediate and long5term benefits
=on+t worr" a$out 877A test coverage. =ownloa Quest 1o e 2ester an give it a tr"; www.unit-test.com
Copyright 2006 Steven

You might also like