920 likes | 1.09k Views
CEN 5070 - Software V & V Unit Testing Concepts. Purpose. This module presents the basic concepts of black-box and white-box testing for unit testing. A systematic approach is shown for deriving functional, boundary and white-box test cases.
E N D
Purpose This module presents the basic concepts of black-box and white-box testing for unit testing. A systematic approach is shown for deriving functional, boundary and white-box test cases. "… arbitrarily selected test set ... results in inefficient testing, leaving some functions untested while performing redundant testing of others." Darlene Mackay, Quality Consultants Unlimited Unit Test Concepts
Agenda • What Is Unit Testing • Black-Box Testing • White-Box Testing • Putting It All Together Unit Test Concepts
WHAT IS UNIT TESTING? • Executing a software element to determine whether it meets its specification • Executing a software element to discover defects or anomalies • Inspecting software element code to discover defects or anomalies. Unit Test Concepts
WHAT IS A UNIT? • Named software element • Separately invokable • Performs single function • Examples • Subprogram or script • Field with validation • Database stored procedure • Java class method Unit Test Concepts
SPRAE: A Model for Testing Practice • Specification -- basis for software testing • Premeditation -- testing requires planning, forethought • Repeatability -- process, results independent of tester • Accountability -- testing artifacts maintained • Economy in the use of human, time and computing resources Unit Test Concepts
Specification Test Strategy/Plan Test Cases Implementation Test Script, Data, Driver Execution Defect Data Problem Reports Test Results Evaluation A TESTING LIFECYCLE Analysis Design Unit Test Concepts
Agenda • What is Unit Testing • Black-Box Testing • White-Box Testing • Putting It All Together Unit Test Concepts
Software under Test BLACK-BOX TESTING • Testing based on the specification rather than the implementation. • Specification defines the expected response(s) to stimuli Stimuli Response(s) Unit Test Concepts
BLACK-BOX TECHNIQUES • Functional testing -- tests the behavior of the software. • Boundary testing -- tests behavior at the lower/upper bounds of input values • Random testing -- tests using randomly generated stimuli (load testing) • Intuitive (ad hoc) testing -- error guessing Unit Test Concepts
FUNCTIONAL TEST DESIGN METHODOLOGY • Specification • Identify behaviors • Develop test cases • Write test script Unit Test Concepts
Software under Test Hours Pay Rate EXAMPLE A(1) Specification • Compute pay for an hourly employee, given the number of hours worked and the hourly pay rate. Compute overtime at 1.5 times hourly rate for hours in excess of 40. Unit Test Concepts
EXAMPLE A(2) Identify Behaviors • Case 1: No overtime (Hours <= 40) • Expect Pay = Hours * Rate • Case 2: Overtime (Hours > 40) • Expect Pay = 40*Rate+1.5*Rate*(Hours - 40) Unit Test Concepts
EXAMPLE A(3) Create Test Cases • Case 1: No overtime (Hours <= 40) • Use Rate = 10, Hours = 30 • Expect Pay = Hours * Rate = 300 • Case 2: Overtime (Hours > 40) • Use Rate = 10, Hours = 50 • Expect Pay = 40*Rate+1.5*Rate*(Hours - 40) = 550 Unit Test Concepts
Stimuli Expected Response Step Hours Rate Pay = 1 30 10 300 2 50 10 550 EXAMPLE A(4) Write Test Script Unit Test Concepts
A MORE COMPLEX EXAMPLE (B) • Increased number of behaviors • Use of decision table to document behaviors • Test case generation from decision table Unit Test Concepts
EXAMPLE B(1) Specification • Compute pay for employee, given the number of hours worked and the hourly pay rate. For hourly employees (rate < 30), compute overtime at 1.5 times hourly rate for hours in excess of 40. Salaried employees (rate >= 30) are paid for exactly 40 hours. Unit Test Concepts
EXAMPLE B(2) Identify Behaviors • Case 1: Hourly AND No overtime • (Rate < 30) & (Hours <= 40) • Expect Pay = Hours * Rate • Case 2: Hourly AND Overtime • (Rate < 30) & (Hours > 40) • Expect Pay = 40*Rate+1.5*Rate*(Hours - 40) • Case 3: Salaried (Rate >= 30) • Expect Pay = 40 * Rate Unit Test Concepts
Condition c1: Rate < 30 | Y Y N N c2: Hours <= 40|Y N Y N Action a1: Pay = Straight time | X a2: Pay = Overtime | X a3: Pay = Professional | X X DECISION TABLE Columns define Behaviors Unit Test Concepts
EXAMPLE B(3) Create Test Cases • One test case per column of decision table • Case 1: Hourly, No Overtime • Case 2: Hourly, Overtime • Case 3: Salaried, No Extra Hours • Case 4: Salaried, Extra Hours • Order the test cases by column Unit Test Concepts
Stimuli Expected Response Step Hours Rate Pay = 1 30 10 300 2 50 10 550 3 30 40 1600 4 50 40 1600 EXAMPLE B(4) Write Test Script Unit Test Concepts
Elementary conditions Condition c1: Rate < 30 | Y Y N N c2: Hours <= 40|Y N Y N Action a1: Pay = Straight time | X a2: Pay = Overtime | X a3: Pay = Professional | X X Use X to select action(s) RULES -- DECISION TABLES Use 'Y', 'N', '-' or space Unit Test Concepts
Your Turn -- Problem P1(1) Specification • Compute the dosage of drug X for patient, given the patient's Age and Weight. For patients 12 and under, the dosage is 1 pill. For patients over 65, the dosage is 2 pills. For all other patients, the dosage is 2 pills plus an extra pill for each 50 pounds above 120. The drug can not be given to patients over 300 pounds or over the age of 80. Unit Test Concepts
Your Turn(2a) Identify Behaviors Expected Case Stimulus Description #Pills 1 2 3 4 5 6 Unit Test Concepts
Your Turn (2b) Decision Table c1: Age <= 12 | c2: Age > 65 | c3: Age > 80 | c4: Weight > 300 | c5: Weight > 120 | a1: Pills = 0 | a2: Pills = 1 | a3: Pills = 2 | a4: Pills = 2+(W-120)/50 | Unit Test Concepts
Your Turn(3) Create Test Cases Case 1 2 3 4 5 6 7 8 9 Age ___ ___ ___ ___ ___ ___ ___ ___ ___ Weight ___ ___ ___ ___ ___ ___ ___ ___ ___ Pills ___ ___ ___ ___ ___ ___ ___ ___ ___ Unit Test Concepts
Stimuli Pills= Step Age Weight 1 2 3 4 5 6 Your Turn(4) Write Test Script Stimuli Pills= Step Age Weight 7 8 9 10 11 12 Unit Test Concepts
SCALING UP The heart of the approach is to use a decision table as a thinking tool. The most critical task in this process is to identify all the stimuli and responses. When there are many logical combinations of stimuli, the decision table can become large, indicating that the unit is probably too complex. Unit Test Concepts
IDENTIFYING BEHAVIORApproaches • Work backwards • Identify each response • Identify conditions that provoke response • Identify separate stimuli • Work forward • Identify each stimulus • Identify how stimulus influences what unit does • Specify the response • Treat stimuli combinations Unit Test Concepts
IDENTIFYING STIMULI • Arguments passed upon invocation • Interactive user inputs • Internal, secondary data • global or class variables • External data (sources) • file or database status variables • file or database data • Exceptions Unit Test Concepts
IT PAYS TO BE A GOOD STIMULUS DETECTIVE • Failure to identify stimuli results in an incomplete, possibly misleading test case • The search for stimuli exposes • interface assumptions -- a major source of integration problems • incomplete design of unit • inadequate provision for exception handling Unit Test Concepts
IDENTIFYING RESPONSES • Arguments/Results passed back on exit • Interactive user outputs • Internal, secondary data • updated global or class variables • External data (sinks) • output file or database status variables • output file or database data • Exceptions Unit Test Concepts
IT PAYS TO BE A GOOD RESPONSE DETECTIVE • Failure to identify responses results in • incomplete understanding of the software under test • shallow test cases • incomplete expected results • incomplete test "success" verification -- certain effects not checked • To test, one must know all the effects Unit Test Concepts
A SKETCHING TOOL Black-Box Schematic Stimulus Type Response Type Argument Argument Inputs Outputs Software under Test Globals Globals Database Database Exception Exception Unit Test Concepts
BEFORE CONTINUTING Much of the discussion so far involves how to identify what software does. We have introduced thinking tools for systematically capturing our findings. These thought processes and tools can be used anywhere in the lifecycle, e.g., in software design! One Stone for Two Birds!! Unit Test Concepts
BOUNDARY TESTING DESIGN METHODOLOGY • Specification • Identify elementary boundary conditions • Identify boundary points • Generate boundary test cases • Update test script (add boundary cases). Unit Test Concepts
Software under Test (1) Specification • Compute pay for an hourly employee, given the number of hours worked and the hourly pay rate. Compute overtime at 1.5 times hourly rate for hours in excess of 40. Hours Pay Rate Unit Test Concepts
(2) Identify Boundary Conditions • Condition 1 (bc1): Hours <= 40 • Observations: • Condition taken directly from decision table • No boundary conditions for Rate Unit Test Concepts
(3) Identify Boundary Points • bc1 (Hrs <= 40) Boundary Points • Point 1: AT the boundary: Hours = 40 • Point 2: Just INside: Hours = 39 • Point 3: Just OUTside: Hours = 41 • Observations: • Inclusive inequalities have 3 boundary points Unit Test Concepts
(3a) BP Generalization • bc x > y has TWO boundary points • bp1: Just INside: x = y + precision • bp2: Just OUTside: x = y • bc x = y has THREE boundary points: • bp1: OUTlo: x = y - precision • bp2: OUThi: x = y + precision • bp3: AT: x = y Unit Test Concepts
(3b) BP Generalization • bc x != y has THREE boundary points: • bp1: INlo: x = y - precision • bp2: INhi: x = y + precision • bp3: OUT: x = y Unit Test Concepts
(4) Generate Test Cases • Combine Hoursboundary points with Rate • Case 1 (AT): Hours = 40, Rate = 10, Pay=400 • Case 2 (IN): Hours = 39, Rate = 10, Pay=390 • Case 3: (OUT): Hours = 41, Rate=10, Pay=415 • Observations: • Test each boundary point individually • Then consider pair-wise boundary points Unit Test Concepts
(5) Update Test Script Stimuli Expected Response Step Hours Rate Pay = 1 30 10 300 2 50 10 550 3 40 10 400 4 39 10 390 5 41 10 415 Unit Test Concepts
c1: Age <= 12 | Y N N c2: Age > 65 | Y N N c3: Age > 80 | Y c4: Weight > 300 | Y N N N N c5: Weight > 120 | N Y a1: Pills = 0 | X X a2: Pills = 1 | X a3: Pills = 2 | X X a4: Pills = 2+(W-120)/50 | X Your TurnBoundary Testing Decision Table: Unit Test Concepts
Your Turn (2-3) Boundary Conditions/Points Boundary Point (BP) Boundary Condition AT IN OUT bc1: bc2: bc3: bc4: bc5: Unit Test Concepts
Your Turn(4) Generate Boundary Test Cases • To create a test case, you must pair an Age with a Weight • Weight boundary point + NOMINAL Age • Age boundary point + NOMINAL Weight • OUT Age + OUT Weight A nominal value is one that is not close to a boundary point. For simplicity, use the same nominal value in all test cases. Unit Test Concepts
Your Turn(4) Boundary-Nominal Test Cases Condition btc BC Weight Age Expect Weight>300 1 IN 301 21 0 2 OUT 300 21 Age <= 12 3 IN 220 11 4 AT 12 5 OUT 13 Age > 65 6 IN 7 OUT Age > 80 9 IN 10 OUT Weight>120 11 IN 12 OUT Unit Test Concepts
Your Turn(4) Out-Out Boundary Test Cases Condition OUT BP Weight Age btc Weight Age Expect >300 <=12 13 >65 14 >80 15 >120 <=12 16 >65 17 >80 18 Unit Test Concepts
OBSERVATIONS • Functional testing defines a minimal number of test cases • Boundary testing adds a large number of test cases, but are EASY to create • Boundary testing finds lots of errors! Unit Test Concepts
BOUNDARIES EXIST FOR • Optional fields: (present, missing). • Variable length data: null string, max length • Database tables: empty • Searching: first/last position • File: closed, empty Unit Test Concepts