Presentation is loading. Please wait.

Presentation is loading. Please wait.

06 Testing selection1June 15 06 Testing selection CE00858-1: Fundamental Programming Techniques.

Similar presentations


Presentation on theme: "06 Testing selection1June 15 06 Testing selection CE00858-1: Fundamental Programming Techniques."— Presentation transcript:

1 06 Testing selection1June 15 06 Testing selection CE00858-1: Fundamental Programming Techniques

2 06 Testing selection2June 15 Objectives In this session, we will: look at testing selections develop test plans and see how to document tests see how if statements are combined into more complex structures explain the importance of braces in selection statements

3 06 Testing selection3June 15 Testing when running programs that accept data we must check results produced by the code not sufficient to assume that if the program outputs some results, those results are correct testing is the process of finding errors debugging is the process of removing errors

4 06 Testing selection4June 15 Testing selections selections provide choice in the code different code is executed dependent on the data must execute every possible path through a program test plan must be designed choose values to ensure all possible paths are executed need to check for values around boundaries with discrete values it is necessary to try each possible value in a separate test

5 06 Testing selection5June 15 Testing simple selections 2 paths through program to check bank balance: must choose data that tests each path: Path 1: balance = 67.39 Path 2: balance = -347.98 //output message giving status of account if ( balance < 0.0 ) { System.out.println("Overdrawn"); } else { System.out.println("In credit"); } Path 1: executed if condition true Path 2: executed if condition false

6 06 Testing selection6June 15 Testing around boundaries errors may occur if the incorrect comparison operator is chosen: less than instead of less than or equal (< or <=) need to check for values around boundaries must choose data that tests around boundary above boundary: balance = 0.0 below boundary: balance = -0.01 //output message giving status of account if ( balance < 0.0 )... Boundary between 0.0 and negative value

7 06 Testing selection7June 15 Testing expressions each comparison in a logical expression needs to be tested must choose data to test each comparison: age >= 18 age < 18 age <= 35 age > 35 //check whether person can go on 18 – 35 holiday if (( age >= 18 ) && (age <= 35))... Need to check all combinations

8 06 Testing selection8June 15 Testing complex conditions need to test each combination of each test must choose data to test each combination: true && true: number = 15 true && false: number = 3 false && true: number = 5 false && false: number = 4 //check whether number is divisible by 3 and 5 if (( number % 3 == 0 ) && (number % 5 == 0))...

9 06 Testing selection9June 15 Selecting data need minimal set of data that will test: all possible paths through the code all comparisons in expressions all possible combinations in logical expressions all boundary conditions must also calculate expected results for comparison against actual results program needs to be run using selected data

10 06 Testing selection10June 15 Documenting testing not sufficient to just run the program with carefully selected data test plan should show: the data to be used the reason for the test the expected result the actual result if actual results are not as expected: program will probably need to be corrected all tests must be rerun

11 06 Testing selection11June 15 Test plan for bank balance program DataReason for testExpected results Actual results balance = 57typical credit balance "In credit"As expected balance = -43typical overdrawn value "Overdrawn"As expected balance = 0lowest value that isn’t overdrawn "In credit"As expected balance = -0.01smallest overdrawn balance "Overdrawn"As expected

12 06 Testing selection12June 15 Multiple selection used to select one of several discrete groups only one alternative can be selected as soon as any condition is true, the statements in the associated block are executed after executing block, control passes to the end of the if statement

13 06 Testing selection13June 15 Multiple selection example – Grades problem: output a grade based on the exam result entered: over 70 is A 60 – 69 is B 50 – 59 is C 40 – 49 is D under 40 is E

14 06 Testing selection14June 15 Grades analysis what data is used? percentage: integer in the range 0 – 100 input by user need selection as different grade output dependent on percentage what operations are done before the selection? create Scanner prompt user for percentage input percentage what operations are done if percentage 70 or over? output "A" what operations are done if percentage 60 - 69? output "B" what operations are done if percentage 50 - 59? output "C what operations are done if percentage 40 - 49? output "D" what operations are done if percentage is none of the above? output " what operations are done after the selection? none

15 06 Testing selection15June 15 //output grade based on percentage Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter percentage: "); int percentage = myKeyboard.nextInt(); if ( percentage >= 70 ) { System.out.println("A"); } else if ( percentage >= 60 ) { System.out.println("B"); } else if ( percentage >= 50 ) { System.out.println("C"); } else if ( percentage >= 40 ) { System.out.println("D"); } else { System.out.println("E"); } executed if condition 1 true executed if condition 1 false and condition 2 true executed if condition 1 and 2 false and condition 3 true executed if condition 1 2 and 3 false and condition 4 true executed if all conditions false Grades.java

16 06 Testing selection16June 15 Testing multiple selections must check each path through multiple selection DataReason for testExpectedActual percentage = 89typical value for "A""A"As expected percentage = 70lowest value for "A""A"As expected percentage = 69highest value for "B""B"As expected percentage = 60lowest value for "B""B"As expected percentage = 59highest value for "B""C"As expected percentage = 50lowest value for "B""C"As expected percentage = 49highest value for "B""D"As expected percentage = 40lowest value for "B""D"As expected percentage = 28typical value for "E""E"As expected

17 06 Testing selection17June 15 Nested selections actions to be performed within a selection can be anything, including other selections nested if statements are if statements within if statements the outer if is evaluated first if true, any inner if is evaluated

18 06 Testing selection18June 15 Nested selection example – Pension problem: determine the user’s eligibility for pension based on their sex and age: males are eligible for pensions from 65 females are eligible for pensions from 60

19 06 Testing selection19June 15 Pension analysis what data is used? gender: string input by user, either "male" or "female" age: positive integer input by user need selection as males and females dealt with in different ways what operations are done before the male / female selection? create Scanner prompt user for gender and age input gender and age what operations are done if gender is male? need nested selection as males 65 or over are treated differently what operations are done before the age selection? none what operations are done if age 65 or over? output "eligible male" what operations are done if age not 65 or over? output "ineligible male" what operations are done after the age selection? none

20 06 Testing selection20June 15 Pension analysis continued what operations are done if gender is not male? need nested selection as females 60 or over are treated differently what operations are done before the age selection? none what operations are done if age 60 or over? output "eligible female" what operations are done if age not 60 or over? output "ineligible female" what operations are done after the age selection? none what operations are done after the male / female selection? none

21 06 Testing selection21June 15 Scanner myKeyboard = new Scanner(System.in); System.out.print("Enter gender: "); String gender = myKeyboard.next(); System.out.print("Enter age: "); int age = myKeyboard.nextInt(); if (gender.equals("Male")) { //check male's age if (age >= 65) { System.out.println("Eligible male"); } else { System.out.println("Ineligible male"); } else { //check female's age if (age >= 60) { System.out.println("Eligible female"); } else { System.out.println("Ineligible female"); } gender tested in outer if statement male age tested in inner if statement female age tested in inner if statement Pension.java

22 06 Testing selection22June 15 Testing nested selections must check each path through nested selection also need to check boundaries for each comparison male 65, male 64, female 60, female 59 DataReasonExpectedActual gender = "male" age = 37 typical value for ineligible male "Ineligible male" As expected gender = "male" age = 85 typical value for eligible male "Eligible male" As expected gender = "female" age = 43 typical value for ineligible female "Ineligible female" As expected gender = "female" age = 74 typical value for eligible female "Eligible female" As expected

23 06 Testing selection23June 15 Braces in if statements braces are not mandatory in if statements but... what is output for: a = 3, b = 3, c = 3? a = 3, b = 4, c = 3? if (a == b) if (a > c) System.out.println("a bigger than c"); else System.out.println("a not equal to b");

24 06 Testing selection24June 15 Matching else else matches the nearest preceding unmatched if in the same block braces used to block statements: if (a == b) { if (a > c) { System.out.println("a bigger than c"); } else { System.out.println("a not equal to b"); }

25 06 Testing selection25June 15 Summary In this session we have: tested selections constructed test plans combined if statements to form multiple and nested selections seen how braces affect if statements In the next session we will: look at further selection using the Switch statement


Download ppt "06 Testing selection1June 15 06 Testing selection CE00858-1: Fundamental Programming Techniques."

Similar presentations


Ads by Google