+ All Categories
Home > Documents > 06 Testing selection1June 15 06 Testing selection CE00858-1: Fundamental Programming Techniques.

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

Date post: 21-Dec-2015
Category:
View: 217 times
Download: 0 times
Share this document with a friend
25
06 Testing selection 1 25 Mar 2022 06 Testing selection CE00858-1: Fundamental Programming Techniques
Transcript
Page 1: 06 Testing selection1June 15 06 Testing selection CE00858-1: Fundamental Programming Techniques.

06 Testing selection 118 Apr 2023

06 Testing selection

CE00858-1: Fundamental Programming Techniques

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

06 Testing selection 218 Apr 2023

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

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

06 Testing selection 318 Apr 2023

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

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

06 Testing selection 418 Apr 2023

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

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

06 Testing selection 518 Apr 2023

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 accountif ( 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

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

06 Testing selection 618 Apr 2023

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 accountif ( balance < 0.0 ) ...

Boundary between 0.0 and negative value

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

06 Testing selection 718 Apr 2023

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 holidayif (( age >= 18 ) && (age <= 35)) ...

Need to check all combinations

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

06 Testing selection 818 Apr 2023

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 5if (( number % 3 == 0 ) && (number % 5 == 0))...

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

06 Testing selection 918 Apr 2023

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

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

06 Testing selection 1018 Apr 2023

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

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

06 Testing selection 1118 Apr 2023

Test plan for bank balance program

Data Reason for test

Expected results

Actual results

balance = 57 typical credit balance

"In credit" As expected

balance = -43 typical overdrawn value

"Overdrawn"

As expected

balance = 0 lowest value that isn’t overdrawn

"In credit" As expected

balance = -0.01

smallest overdrawn balance

"Overdrawn"

As expected

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

06 Testing selection 1218 Apr 2023

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

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

06 Testing selection 1318 Apr 2023

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

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

06 Testing selection 1418 Apr 2023

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

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

06 Testing selection 1518 Apr 2023

//output grade based on percentageScanner 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

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

06 Testing selection 1618 Apr 2023

Testing multiple selections

• must check each path through multiple selection

Data Reason for test Expected

Actual

percentage = 89

typical value for "A"

"A" As expected

percentage = 70

lowest value for "A"

"A" As expected

percentage = 69

highest value for "B"

"B" As expected

percentage = 60

lowest value for "B"

"B" As expected

percentage = 59

highest value for "B"

"C" As expected

percentage = 50

lowest value for "B"

"C" As expected

percentage = 49

highest value for "B"

"D" As expected

percentage = 40

lowest value for "B"

"D" As expected

percentage = 28

typical value for "E"

"E" As expected

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

06 Testing selection 1718 Apr 2023

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

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

06 Testing selection 1818 Apr 2023

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

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

06 Testing selection 1918 Apr 2023

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

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

06 Testing selection 2018 Apr 2023

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

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

06 Testing selection 2118 Apr 2023

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 ageif (age >= 65){

System.out.println("Eligible male");}else{

System.out.println("Ineligible male");}

}else{

//check female's ageif (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

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

06 Testing selection 2218 Apr 2023

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

Data Reason Expected Actual

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

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

06 Testing selection 2318 Apr 2023

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");

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

06 Testing selection 2418 Apr 2023

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");}

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

06 Testing selection 2518 Apr 2023

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


Recommended