Six Simple Steps To Unit Testing Happiness
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
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
"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
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
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.
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.
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 .
(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));
(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.
+ 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>
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
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-!*
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
*.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;
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