An Introductory Acceptance Test

Size: px
Start display at page:

Download "An Introductory Acceptance Test"

Transcription

1 4 An Intrductry Acceptance Test "If yu dn't knw where yu are ging, yu will wind up smewhere else." Ygi Berra The Triad Tm, Debbie, and Cathy are in their secnd meeting tgether. Debbie describes an example f an acceptance test and fur ways that an acceptance test can be executed. An Example Business Rule Debbie, Tm, and Cathy cntinue their discussin f Acceptance Test-Driven Develpment. Debbie is talking abut a previus prject where she and Tm created tests in cllabratin with the custmer. The business representative, Betty, presented us with a business rule fr giving discunts that she had btained frm ne f the stakehlders. The stakehlder wanted t give discunts t the firm s custmers. The discunt was t vary based n the type f custmer. We had already cmpleted implementing a previus requirement that determined the type f custmer. Here s the rule that Betty gave t us: ***Prductin Nte - indentatin in next paragraph is nn-standard If Custmer Type is Gd and the Item Ttal is less than r equal $10.00, Then d nt give a discunt, Otherwise give a 1% discunt. If Custmer Type is Excellent, Then give a discunt f 1% fr any rder. If the Item Ttal is greater than $50.00, Then give a discunt f 5%. This rule seems pretty clear. It uses cnsistent terms such as Custmer Type and Item Ttal. We had previusly gtten frm Betty the definitins f thse terms. (** Ref t Ubiquitus Language and Dmain Driven Design**) Fr example, the Item Ttal did nt include any taxes r shipping. But even with that cnsistency, we had an issue. Tm and I lked at the rule and tried t figure ut what the discunt percentage shuld be if a custmer wh is gd had an rder ttal greater than $ Then the three f us made up a table f examples. Item Ttal Custmer Rating percentage? Gd Gd Gd 1.0??.01 Excellent Excellent

2 50.01 Excellent 5.0 Debbie cntinues, The first tw rws shw that the limit between giving a Gd custmer a discunt r a 1.0% discunt is $ The less than r equal in the business rule is pretty clear. But we wanted a test t ensure that my implementatin prduced that result. We put a?? after the 1.0 in the third example, since it was unclear t us whether that was the right value. The furth example indicates that we understand that the discunt fr an Excellent custmer starts at the smallest pssible Item Ttal. The fifth and sixth entries shw that the discunt increases just after the $50.00 pint. Betty tk this table back t the stakehlder. He lked it ver and said that the interpretatin was crrect. He did nt want t give a 5% discunt t Gd custmers. S we remved the?? frm that result. We nw had a set f tests that we culd apply t the system. The crrect discunt amunt tests is nt just a single case, but includes cases fr all pssible cmbinatins Tm interjects, But these were nt all the tests. Being trained as a tester, I like t cnsider ther pssibilities. Fr example, what if the Item Ttal was less than $0.00? I asked Betty whether this wuld ever happen. She said it might be pssible, since the Item Ttal culd include a rebate cupn that was greater than the ttal f the items. S I added the fllwing pssibilities: Item Ttal Custmer Rating percentage? -.01 Gd Excellent 1.0?? Tm explains, It didn t seem right t apply a discunt percentage that wuld actually increase the amunt that was charged t the custmer. Based n this example, Betty went back t the stakehlder and cnfirmed that the percentage shuld be 0% if the Item Ttal is less than 0 fr any custmer. These examples were the acceptance tests fr the system. If Debbie implemented these crrectly, Betty wuld be satisfied. Tm cntinued, Nw it was a matter f hw we were ging t use these tests t test the system. Implementing the Acceptance Tests Debbie states, Tm and I needed t apply these tests t my implementatin. There are at least fur ways we culd d this. First, Tm culd create a test script that perates manually at the user interface level. Secnd, I culd create a test user interface that allws me r Tm t check the apprpriate discunt percentages. Third, I culd perfrm the tests using a unit testing framewrk. A standard unit testing framewrk is generically called XUnit. Furth, Tm and I culd implement the tests with an acceptance test framewrk. Let me shw yu sme examples f hw we culd have dealt with each f these ways. Test script Debbie cntinues, In this case, the prgram has a user interface that allws a custmer t enter an rder. The user interface flw is much like Amazn r ther rder sites. The user enters an rder and a summary screen appears, such as this ne: (**Nte: Make int a screen lk-alike ***) Order 32

3 Summary Cunt Item Item Price Ttal 10 Little Widget $.10 $ Big Widget $9.00 $9.00 Item Ttal $10.00 $0.00 Taxes $.55 Shipping $2.00 Order Ttal $12.55 Nw what Tm wuld have t d is create a script that either he r I wuld fllw in rder t test each f the six examples. He might start by cmputing what the actual discunt amunt shuld be fr each case. Unless the rder summary screen shws this percentage, this is the nly utput he can check t make sure the calculatin is crrect. S here s the additin t the table: Item Ttal Custmer Rating percentage? Gd Amunt? Ntes Gd runded dwn Gd runded dwn.01 Excellent runded dwn Excellent Excellent runded dwn The script wuld g smething like this: 1. Lgn as a Custmer wh has the Rating listed in the table. 2. Start an rder and put items in it until the ttal is the specified amunt in the Item Ttal clumn n the test. 3. Check that the discunt n the Order Summary screen matches Amunt in the table. Then the test wuld be repeated five mre times fr all six cases. Either he r I wuld d this nce I ve cmpleted implementing the discunt feature. Tm interrupted, Of curse, I d want Debbie t d this befre she turned the prgram ver t me. Better fr her t get carpal tunnel syndrme, then fr me. Debbie glanced ver at Tm with a wry smile n her face. She cntinued, Actually, neither f us wants t get carpal tunnel syndrme. It s bad fr ur tennis game. This test that checks fr the crrect discunt amunt needs t be run, but nt fr all the pssible cmbinatins. Yu can imagine what might have happened if there were ten discunt percentages fr each f ten different custmer types. I d definitely have let Tm d the tests s his hand wuld nt be in shape fr ur next tennis match. S let s lk at the next pssible way t run these tests. Testing User Interface T simplify executing the tests, I culd set up a user interface that cnnected t the discunt calculatin mdule in my cde. This interface wuld nly be used during testing. But having it wuld 33

4 cut dwn n the wrk invlved in shwing that the percentage was crrectly determined. The user interface might lk like: (**Nte: Make int a UI like display **) Percentage Test Custmer Type Gd Item Ttal Percentage 1 % With this user interface, Tm r I culd mre quickly enter in all the cmbinatins that are shwn in the test table. It wuld cut dwn n the pssibility f repetitive mtin injuries. Our wrkman s cmpensatin insurance likes that idea. We wuld still need t run Tm s riginal script fr a cuple f instances t make sure that it was prperly cnnected up t the discunt percentage mdule. But unless there was a large risk factr invlved, yu might just run it fr a few cases such as: Item Ttal Custmer Rating percentage? Amunt? Gd Excellent This user interface has penetrated int the system. It expses a test pint within the system that allws easier testing. Let me give yu an analgy shwing the differences between this methd and Tm s riginal test script. Suppse yu want t build a car that accelerates quickly. Yu knw yu need an engine that can increase its speed rapidly. If yu culd nly check the engine peratin as part f the car, yu wuld need t put the engine in the car and then take the car n a test drive. If yu had a test pint fr the engine speed inside the car, yu culd check hw fast it sped up withut driving the car. Yu culd measure it in the garage. Yu d save a lt f time in n-the-rad testing if the engine wasn t wrking prperly. That desn t mean yu dn t need t test the engine n the rad. But if the engine isn t wrking by itself, yu dn t run the rad test until the engine passes its wn tests. If yu re nt int cars, let me give a cntext diagram, as belw. The Order Summary Screen cnnects t the system thrugh the standard user interface layer. The Percentage Screen cnnects t sme mdule inside the system. Let s call that mdule the Calculatr. By having a cnnectin t the inside, a tester can check whether the internal behavir by itself is crrect. Order Summary Screen Interir f applicatin, Calculatr User Interface Percentage Screen XUnit Testing 34

5 The next way I culd perfrm the testing is t write unit tests fr the discunt calculatr. These unit tests are usually written in the prgram language. Here s a sample f what these tests lk like. I knw yu re nt a prgrammer. But as yu can see, it can be a little hard t see exactly what is being tested. } class CalculatrTest { testpercentagefrcustmer() { Calculatr dc = new Calculatr() assertequals(0.0, dc.cmputepercentage(10.0, Custmer.Gd)); assertequals(1.0, dc.cmputepercentage (10.01, Custmer.Gd)); assertequals(1.0, dc.cmputepercentage (50.01, Custmer.Gd)); assertequals(1.0, dc.cmputepercentage(.01, Custmer.Excellent)); assertequals(1.0, dc.cmputepercentage(50.0, Custmer.Excellent)); assertequals(5.0, dc.cmputepercentage(50.01, Custmer.Excellent)); } Luckily Tm has dne sme prgramming, s he can read the cde. Debbie quickly shts a smile at Tm. Hwever any time we make a change in the tests that Betty and the stakehlder can read, I als have t make a change in these tests. That s a little bit f waste. And t effectively prduce sftware, we d like t eliminate that waste if pssible. S Betty, Tm, and I selected the next mde f testing. Autmated Acceptance Testing The three f us agreed that the examples in the table accurately reflected the requirements and there wuld be less waste if the table did nt have t be cnverted int anther frm fr testing. There are several available test framewrks that can use this table r a slightly altered frm f this table t directly drive the tests. I m nt ging t get int the details f them at this time. The authr is ging t have references t them later n in this bk. (** Frward Reference here**) What I wuld like t shw is hw we use ne f these framewrks. The ne we use in this example is the Framewrk fr Integrated Tests r knwn as Fit. It was develped by Ward Cunningham. (** Cite references t fit.c2.cm and R Mugridge & W Cunningham, Fit fr Develping Sftware: Framewrk fr Integrated Tests, Prentice Hall PTR (2005). The prgram is nw crdinated by James Shre.) With Fit, yu describe the tests with a table similar t the ne we used with Betty. I m nt ging t get int the syntactic details r therwise it might cnfuse yu at this pint. I ll use the same table that we started with. And besides, we re nt trying t push a particular test framewrk. S getting int the minute details is beynd the scpe f this bk. Here s the test. It lks just like the table that Betty presented t the stakehlder. Item Ttal Custmer Rating percentage? Gd Gd Gd Excellent

6 50.00 Excellent Excellent 5.0 Nw when we run this table as a test, Fit executes cde that cnnects up t the Calculatr. It gives the Calculatr the values in Item Ttal and Custmer Rating. The Calculatr returns the Percentage. Fit cmpares the returned value t the value in the table. If it agrees, the clumn shws up in green. If it des nt, it shws up as red. Yu can t see the clrs in black and white. S light grey represents green and dark grey represents red. The first time I ran the test, I gt the fllwing table as the utput f Fit. (** Nte: Make the table have frmatting that lks different fr these rws. Item Ttal Custmer Rating percentage? Gd Gd Gd Expected 1.0 Actual Excellent Excellent Excellent 5.0 Tm winks and says, With this table as the results, it was clear that Debbie had an issue with the Calculatr. That avided my wasting time telling her smething she already knew. Like hw I m ging t beat her in the next tennis turnament. Debbie glanced back and saw a grin n Tm s face. She cntinued, Tm didn t even see this. Like my last serve against him. I saw that there was miscding n my part. I fixed it. When all the tests passed, I turned it ver t Tm. Betty saw the passing tests as cnfirmatin that the calculatin was wrking as desired. Summary Examples f requirements clarify the requirements The examples can be used as tests fr the implementatin f the requirements Tests fr business rules can be executed in many ways Creatin thrugh user interface f transactin that invkes business rule Develpment f a user interface that invkes the business rule directly A unit test implemented in a language s unit testing framewrk A autmated test that talks t the business rule mdule 36