Software Testing and Reliability Functional Testing Aditya P
Software Testing and Reliability Functional Testing Aditya P. Mathur Purdue University May 19 -23, 2003 @ Guidant Corporation Minneapolis/St Paul, MN Graduate Assistants: Ramkumar Natarajan Baskar Sridharan Last update: April 17, 2003 Functional testing
Learning Objectives n What is functional testing? n How to perform functional testing? n n n What are clues, test requirements, and test specifications? How to generate test inputs? What are equivalence partitioning, boundary value testing, domain testing, state testing, and decision table testing? Software Testing and Reliability Aditya P. Mathur 2003 2
References n The Craft of Software Testing, Brian Marick, Prentice Hall, 1995. Software Testing and Reliability Aditya P. Mathur 2003 3
What is Functional Testing? n n When test inputs are generated using functional specifications, we say that we are doing functional testing. Functional testing tests how well a program meets the functionality requirements. Software Testing and Reliability Aditya P. Mathur 2003 4
The Methodology n The derivation of test inputs is based on functional specifications. n Clues are obtained from the specifications. n Clues lead to test requirements. n Test requirements lead to test specifications. n Test specifications are then used to actually execute the program under test. Software Testing and Reliability Aditya P. Mathur 2003 5
Test Methodology Specifications Clues Expected behavior Test requirements Test specifications Until specs. Exhausted. Test driver Program Software Testing and Reliability Aditya P. Mathur 2003 Oracle Actual behavior Program output is correct or Program has failed; make a note and proceed with testing or get into the debug mode. 6
Specifications n Inputs and tasks: n Given inputs n Perform tasks Software Testing and Reliability Aditya P. Mathur 2003 7
Specifications (contd. ) n Input properties n Input n must satisfy n Function f is a pre-condition on input Software Testing and Reliability Aditya P. Mathur 2003 8
Specifications (contd. ) n Two types of pre-conditions are considered: n Validated: those that are required to be validated by the program under test and an error action is required to be performed if the condition is not true. n Assumed: those that are assumed to be true and not checked by the program under test. Software Testing and Reliability Aditya P. Mathur 2003 9
Specification: Example n For the sort program: n Inputs are: n N n pointer to a sequence of length N n pointer to an area in memory where the output sequence is to be placed. Software Testing and Reliability Aditya P. Mathur 2003 10
Specification: Example (contd. ) n Tasks to be performed: n Sort the sequence in ascending order n Return 1 if sorting is successful, -1 otherwise. Software Testing and Reliability Aditya P. Mathur 2003 11
Preconditions for sort n n Validated: n N>0 n On failure return -1; sorting considered unsuccessful. Assumed: n The input sequence contains N integers. n The output area has space for at least N integers. Software Testing and Reliability Aditya P. Mathur 2003 12
Deriving pre-conditions n n Pre-conditions result from properties of inputs. Example: n alpha_sequence(name) alpha_sequence is the string obtained from name by removing all characters other then A-Z and a-z. Thus, if name is “A 12 C” then alpha_name is “AC”. Software Testing and Reliability Aditya P. Mathur 2003 13
Deriving pre-conditions (contd. ) n n This leads to the following pre-condition: n Validated: the string alpha_sequence(name) is identical to or shorter than the string name. n On failure: print “invalid name”. This property could also lead to the pre-condition: n Assumed: the string alpha_ sequence(name) is identical to or shorter than the string name. Software Testing and Reliability Aditya P. Mathur 2003 14
Post-conditions n n n A post-condition specifies a property of the output of a program. The general format of a post-condition is: n if condition then effect-1 {else effect-2} Example: n For the sort program a post-condition is: n if N>0 then {the output sequence has the same elements as in the input sequence and in ascending order. } Software Testing and Reliability Aditya P. Mathur 2003 15
Post-condition (contd. ) n This could be stated more formally as: n if N>0 then { and each is a member of the input sequence and sort returns 1. } else { the output sequence is undefined and sort returns -1. } Software Testing and Reliability Aditya P. Mathur 2003 16
Post-condition (contd. ) n n Another example: n if (A=B) and (B=C) then return “equilateral”; Can you complete the above post-condition for a program that is required to classify a triangle given the length of three sides? Convention: Do not nest if-then-else conditions while specifying a postcondition. Software Testing and Reliability Aditya P. Mathur 2003 17
Incompleteness of Specifications n n Specifications may be incomplete or ambiguous. Example post-condition: if user places cursor on the name field then read a string n This post-condition does not specify any limit on the length of the input string hence is incomplete. Software Testing and Reliability Aditya P. Mathur 2003 18
Ambiguous Specifications n It also does not make it clear as to n whether a string should be input only after the user has placed the cursor on the name field and clicked the mouse or simply placed the cursor on the name field. and hence is ambiguous. Software Testing and Reliability Aditya P. Mathur 2003 19
Clues: Summary n Clues are: n Pre-conditions n Post-conditions n Variables, e. g. A is a length implying thereby that its value cannot be negative. n Operations, e. g. “search a list of names” or “find the average of total scores” n Definitions, e. g. “filename(name) is a name with no spaces. ” Software Testing and Reliability Aditya P. Mathur 2003 20
Clues (contd. ) n n n Ideally, variables, operations and definitions should be a part of at least one pre- or post-condition. However, this may not be the case as specifications are not always written formally. Hence look out for variables, operations, and definitions within a specification! Software Testing and Reliability Aditya P. Mathur 2003 21
Test Requirements n n A test requirement is a description of how to test the program that is under test. Here is a sample test requirement for a program that classifies a triangle given the length of three sides. n n n A, B, C are non-zero and positive. One of A, B, C is negative; error condition. One of A, B, C is zero; error condition. Software Testing and Reliability Aditya P. Mathur 2003 22
Test Requirements-Derivation n Test requirements are derived from clues. For example, consider the following pre-conditions (clues): n Assumed: A, B, and C are lengths n Validated: A>0, B>0, C>0 These pre-conditions on A, B, and C lead to the test requirement given on the previous slide. Software Testing and Reliability Aditya P. Mathur 2003 23
Test Requirements-Derivation n n Note that we have combined the pre-condition for each input variable into one condition. This is done only for inconvenience! It is recommended that pre-conditions be separated for each variable. Software Testing and Reliability Aditya P. Mathur 2003 24
Test Requirements-Derivation n n Note also that each validated pre-condition results in at least two requirements: one for the validated part and the other for the failure part. In our example above we did not list all requirements. For example, we are content with testing “one of A, B, C is negative; error condition. ” Software Testing and Reliability Aditya P. Mathur 2003 25
Test Requirements-Derivation n Post-conditions also lead to test requirements. n For example, the partial post-condition: n if (A=B) and (B=C) then return “equilateral” leads to the following test requirement: n A=B and B=C. Software Testing and Reliability Aditya P. Mathur 2003 26
Compound Validated Pre-conditions n Compound pre-conditions are ones that use one or more and or connectors in some combination. n Examples: validated compound pre-conditions: n Pre-condition: A and B n Pre-condition: user places the mouse over the name field and clicks it. Software Testing and Reliability Aditya P. Mathur 2003 27
Compound Validated Pre-conditions n n The first of the above pre-conditions leads to four requirements: n A true, B true (This is the validated part) n A false, B true(This and the remaining are failures) n A true, B false n A false, B false You may work out the requirements for compound pre-condition with the or connector. Software Testing and Reliability Aditya P. Mathur 2003 28
Compound Validated pre-conditions n Compound validated pre-conditions could become quite complex. n Example: (A and (B or C)) n Brute force method will lead to 8 test requirements. Software Testing and Reliability Aditya P. Mathur 2003 29
Compound Validated pre-conditions n In general this will lead to too many test requirements. n We can prune them by leaving out those requirements that are unlikely to reveal a program error. n For example, consider the validated pre-condition: A or B. Software Testing and Reliability Aditya P. Mathur 2003 30
Pruning Test Requirements n n There are four possible test requirements: n A true, B true n A false, B true n A true, B false n A false, B false Consider a correct C implementation: if (!(A || B)) exit_with_error(“Error: A is %d, B is %d”, A, B); else. . {/* the validated code comes here. */} Software Testing and Reliability Aditya P. Mathur 2003 31
Possible Errors n Programmer forgets to check for one of the two (A and B) cases resulting in the code: n if (!A) exit_with_error(“Error: A is %d, B is %d”, A, B); or n if (!B) exit_with_error(“Error: A is %d, B is %d”, A, B); Software Testing and Reliability Aditya P. Mathur 2003 32
Possible Errors (contd. ) n n Or, the programmer uses a wrong logical operator as in: if (!(A && B)) exit_with_error(“Error: A is %d, B is %d”, A, B); Let us analyze how the four different tests will perform in each of the four implementations: one correct, and three incorrect ones. Software Testing and Reliability Aditya P. Mathur 2003 33
Truth table: or condition Inputs Incorrect implementations Correct implementation A B !(A || B) !(A&&B) !A !B T F F F T T T F F Notice this one: will it help find any of the three possible errors? Software Testing and Reliability Aditya P. Mathur 2003 34
Truth Table Analysis Case 1: n A test input with A=true and B=false will cause the correct program to evaluate the condition to false. n The two incorrect implementations, !(A&&B) and (!B) will evaluate the condition to true. Software Testing and Reliability Aditya P. Mathur 2003 35
Truth Table Analysis (contd. ) n n Both incorrect implementations will print the error message. The oracle will observe that the correct and the incorrect implementations behave differently. It will therefore announce failure for each incorrect implementation thereby pointing to an error in the code. End of Case 1. Software Testing and Reliability Aditya P. Mathur 2003 36
Truth Table Analysis (contd. ) Case 2: n Test input A=false and B=true will reveal the error in the two incorrect implementations, !(A&&B) and (!A). Case 3: n Test input A=false and B=false might find a fault in then branch of the if condition. Software Testing and Reliability Aditya P. Mathur 2003 37
Truth Table Analysis (contd. ) Case 4: n Test input A=true and B=true might find a fault in the else branch of the if condition. n Thus, all four test inputs are likely to be useful. Software Testing and Reliability Aditya P. Mathur 2003 38
Truth Table Analysis (contd. ) n n However, if we were to check for the correct implementation of the condition A or B, then only the first two inputs are necessary. In this example, reducing the number of test specifications from 4 to 2 does not lead to any significant savings. When will the savings be significant? Software Testing and Reliability Aditya P. Mathur 2003 39
Assumed pre-conditions n n Each assumed pre-condition is likely to result in a test requirement. Example: n Assumed: MODE is “on ground” or “flying” n This leads to two requirements: n MODE is “on ground” , MODE is not “flying” n MODE is not “on ground” , MODE is “flying” Software Testing and Reliability Aditya P. Mathur 2003 40
Assumed pre-conditions n These can be simplified to: n n MODE is “on ground” MODE is “flying” Software Testing and Reliability Aditya P. Mathur 2003 41
Clues from code? n n n Yes, clues can also be derived by scanning the code. However, such clues might be redundant and incomplete if coverage measurement and its use is planned. In the absence of coverage measurement, it is a good idea to scan the code and find clues. Software Testing and Reliability Aditya P. Mathur 2003 42
Clues from code (contd. ) n n Examine internal variables. These may lead to new test requirements. Example: Suppose that variable length is input and denotes the length of an array. In the code we find: int last_index=length+1; This leads to the following test requirements: Software Testing and Reliability Aditya P. Mathur 2003 43
Clues from code (contd. ) n n an array of length zero n array of length 1 n array with more than one element Later we will see how these clues and requirements might be derived, with more certainty, using boundary-value analysis. n Another example: Consider the sort program for which we have seen the specifications. Software Testing and Reliability Aditya P. Mathur 2003 44
Clues from Code (contd. ) n The specifications do not indicate what algorithm is to be used for sorting the input array. n A programmer might decide to use different algorithms for different array sizes. When scanning the code we may see: n if (scan<min_length) simple_sort(); else quicksort(); Software Testing and Reliability Aditya P. Mathur 2003 45
Clues from Code (contd. ) n Variable size and the check against min_length give us a clue for new test requirements. These are: n n size is equal to or greater than min_length Later we will see how this clue and requirements might be derived, with certainty, using branch coverage. Software Testing and Reliability Aditya P. Mathur 2003 46
Test Requirements Checklist n Obtaining clues and deriving test requirements can become a tedious task. n To keep it from overwhelming us it is a good idea to make a checklist of clues. n This checklist is then transformed into a checklist of test requirements by going through each clue and deriving test requirements from it. Software Testing and Reliability Aditya P. Mathur 2003 47
Test Specifications n n A test requirement indicates “how” to test a program. But it does not provide exact values of inputs. A test requirement is used to derive test specification, which is the exact specification of values of input and environment variables. Software Testing and Reliability Aditya P. Mathur 2003 48
Test specifications (contd. ) n n n There might not be a one-to-one correspondence between test requirements and test specifications. A test requirement checklist might contain 50 entries. These might result in only 22 test specifications. The fewer the tests the better but only if these tests are of good quality ! Software Testing and Reliability Aditya P. Mathur 2003 49
Test specifications (contd. ) n n We will discuss test quality when discussing test assessment. A test specification looks like this: n n Test 2: n global variable all_files is initially false. n next_record is set to 1. Upon return expect: n n all_files to be true next_record is last_record+1 Software Testing and Reliability Aditya P. Mathur 2003 50
Test specifications (contd. ) n Notice the format of a test specification: n Each test is given a number which serves as its identifier. n There is a set of input values. n There is a set of expected values upon return from execution. Any side effects on files or networks must also be specified here. In essence, all observable effects must be specified in the “Expect” part of a test specification. Software Testing and Reliability Aditya P. Mathur 2003 51
Test Specifications (contd. ) n n Any side effects on files or networks must also be specified. In essence, all observable effects must be specified in the “Expect” part of a test specification. Similarly, values of all input variables, global or otherwise, must also be specified. Software Testing and Reliability Aditya P. Mathur 2003 52
Test Requirements to Specifications n n n The test requirements checklist guides the process of deriving test specifications. Initially all entries in the checklist are unmarked or set to 0. Each time a test is generated from a requirement it is marked or the count incremented by 1. Software Testing and Reliability Aditya P. Mathur 2003 53
Test Requirements to Specifications n n Thus, at any point in time, one could assess the progress made towards the generation of test specifications. One could also determine how many tests have been generated using any test requirement. Software Testing and Reliability Aditya P. Mathur 2003 54
Test Requirements to Specifications n n Once a test requirement has been marked or its count is more than 0 we say that it has been satisfied. Some rules of thumb to use while designing tests: n Try to satisfy multiple requirements using only one test. n Satisfy all test requirements. Software Testing and Reliability Aditya P. Mathur 2003 55
Test Requirements to Specifications n Avoid reuse of same values of a variable in different tests. Generating new tests by varying an existing one is likely to lead to tests that test the same part of the code as the previous one. In testing, variety helps! n Though we try to combine several test requirements to generate one test case, this is not advisable when considering error conditions. Software Testing and Reliability Aditya P. Mathur 2003 56
Test Requirements to Specifications n For example, consider the following: n speed_dial, an interval n n n speed_dial<0 , error speed_dial>120, error zones, an interval n n zones <5, error zones>10, error Software Testing and Reliability Aditya P. Mathur 2003 57
Test Requirements to Specifications n One test specification obtained by combining the two requirements above is: n n n speed_dial=-1 zone=3 Now, if the code to handle these error conditions is: if (speed_dial<0 || speed_dial>120) error_exit(“Incorrect speed_dial”); if (zone<6 ||zone>10) error_exit(“Incorrect zone”); Software Testing and Reliability Aditya P. Mathur 2003 error 58
Test Requirements to Specifications n n n For our test, the program will exit before it reaches the second if statement. Thus, it will miss detecting the error in coding the test for zone. Also, do not assume an error test to satisfy any other test requirement. Example: n Consider the function: n myfunction(int X, int Y); n A test for the erroneous value of X might not test the code that uses Y. Software Testing and Reliability Aditya P. Mathur 2003 59
Test Requirements to Specifications n Test specifications must not mention internal variables. Remember, a test specification aids in setting input variables to suitable values before the test begins. Values of internal variables are computed during program execution. n However, there are exceptions to the above rule. Can you think of one ? Software Testing and Reliability Aditya P. Mathur 2003 60
Equivalence Partitioning n n n The input domain is usually too large for exhaustive testing. It is therefore partitioned into a finite number of sub-domains for the selection of test inputs. Each sub-domain is known as an equivalence class and serves as a source of at least one test input. Software Testing and Reliability Aditya P. Mathur 2003 61
Equivalence Partitioning Input domain partitioned into four sub-domains. 2 1 3 4 Too many test inputs. Software Testing and Reliability Aditya P. Mathur 2003 Four test inputs, one selected from each sub-domain. 62
How to partition? n Inputs to a program provide clues to partitioning. n Example 1: n n Suppose that program P takes an integer X as input X. For X<0 the program is required to perform task T 1 and for X>=0 task T 2. Software Testing and Reliability Aditya P. Mathur 2003 63
How to partition? (contd. ) n n The input domain is prohibitively large because X can assume a large number of values. However, we expect P to behave the same way for all X<0. Similarly, we expect P to perform the same way for all values of X>=0. We therefore partition the input domain of P into two subdomains. Software Testing and Reliability Aditya P. Mathur 2003 64
Two sub-domains One test case: X=-3 Equivalence class X<0 X>=0 Equivalence class Another test case: X=-15 All test inputs in the X<0 sub-domain are considered equivalent. The assumption is that if one test input in this sub-domain reveals an error in the program, so will the others. This is true of the test inputs in the X>=0 sub-domain also. Software Testing and Reliability Aditya P. Mathur 2003 65
Non-overlapping Partitions n n In the previous example, the two equivalence classes are nonoverlapping. In other words the two sub-domains are disjoint. When the sub-domains are disjoint, it is sufficient to pick one test input from each equivalence class to test the program. Software Testing and Reliability Aditya P. Mathur 2003 66
Non-overlapping Partitions n n An equivalence class is considered covered when at least one test has been selected from it. In partition testing our goal is to cover all equivalence classes. Software Testing and Reliability Aditya P. Mathur 2003 67
Overlapping Partitions n Example 2: n Suppose that program P takes three integers X, Y and Z. It is known that: n n X<Y Z>Y Software Testing and Reliability Aditya P. Mathur 2003 68
Overlapping partitions X<Y, Z<=Y X=2, Y=3, Z=1 X>=Y, Z<=Y X=15, Y=4, Z=1 X<Y, Z>Y X=3, Y=4, Z=7 X>=Y Z>Y Z<=Y X>=Y, Z>Y X=15, Y=4, Z=7 Software Testing and Reliability Aditya P. Mathur 2003 69
Overlapping Partition-Test Selection n n In this example, we could select 4 test cases as: n X=4, Y=7, Z=1 satisfies X<Y n X=4, Y=2, Z=1 satisfies X>=Y n X=1, Y=7, Z=9 satisfies Z>Y n X=1, Y=7, Z=2 satisfies Z<=Y Thus, we have one test case from each equivalence class. Software Testing and Reliability Aditya P. Mathur 2003 70
Overlapping Partitions-Test Selection n n However, we may also select only 2 test inputs and satisfy all four equivalence classes: n X=4, Y=7, Z=1 satisfies X<Y and Z<=Y n X=4, Y=2, Z=3 satisfies X>=Y and Z>Y Thus, we have reduced the number of test cases from 4 to 2 while covering each equivalence class. Software Testing and Reliability Aditya P. Mathur 2003 71
Partitioning using non-numeric data n n In the previous two examples the inputs were integers. One can derive equivalence classes for other types of data also. Example 3: n Suppose that program P takes one character X and one string Y as inputs. P performs task T 1 for all lower case characters and T 2 for upper case characters. Also, it performs task T 3 for the null string and T 4 for all other strings. Software Testing and Reliability Aditya P. Mathur 2003 72
Partitioning using non-numeric data X: lc, Y: not null X: UC, Y: not null X: lc X: UC X: lc, Y: null Y: not null X: UC, Y: null Software Testing and Reliability Aditya P. Mathur 2003 lc: Lower case character UC: Upper case character null: null string. 73
Non-numeric Data n n Once again we have overlapping partitions. We can select only 2 test inputs to cover all four equivalence classes. These are: n X: lower case, Y: null string n X: upper case, Y: not a null string Software Testing and Reliability Aditya P. Mathur 2003 74
Guidelines for equivalence partitioning n Input condition specifies a range: create one for the valid case and two for the invalid cases. n e. g. for a<=X<=b the classes are n a<=X<=b (valid case) n X<a and X>b (the invalid cases) Software Testing and Reliability Aditya P. Mathur 2003 75
Guidelines (contd. ) n Input condition specifies a value: create one for the valid value and two for incorrect values (below and above the valid value). This may not be possible for certain data types, e. g. for boolean. n Input condition specifies a member of a set: create one for the valid value and one for the invalid (not in the set) value. Software Testing and Reliability Aditya P. Mathur 2003 76
Sufficiency of Partitions n In the previous examples we derived equivalence classes based on the conditions satisfied by the input data. n Then we selected just enough tests to cover each partition. n Think of the advantages and disadvantages of this approach ! Software Testing and Reliability Aditya P. Mathur 2003 77
Boundary Value Analysis (BVA) n n n Another way to generate test cases is to look for boundary values. Suppose a program takes an integer X as input. In the absence of any information, we assume that X=0 is a boundary. Inputs to the program might lie on the boundary or on either side of the boundary. Software Testing and Reliability Aditya P. Mathur 2003 78
BVA (contd. ) n This leads to 3 test inputs: n X=0, X=-20, and X=14. Note that the values -20 and 14 are on either side of the boundary and are chosen arbitrarily. n Notice that using BVA we get 3 equivalence classes. One of these three classes contains only one value (X=0), the other two are large! Software Testing and Reliability Aditya P. Mathur 2003 79
BVA (contd. ) n Now suppose that a program takes two integers X and Y and that x 1<=X<=x 2 and y 1<=Y<=y 2. y 2 11 8 y 1 5 1 12 4 x 1 Software Testing and Reliability Aditya P. Mathur 2003 14 2 10 9 13 7 6 3 x 2 80
BVA (contd. ) n n In this case the four sides of the rectangle represent the boundary. The heuristic for test selection in this case is: n n Select one test at each corner (1, 2, 3, 4). Select one test just outside of each of the four sides of the boundary (5, 6, 7, 8) Software Testing and Reliability Aditya P. Mathur 2003 81
BVA (contd. ) n n Select one test just inside of each of the four sides of the boundary (10, 11, 12, 13). n Select one test case inside of the bounded region (9). n Select one test case outside of the bounded region (14). How many equivalence classes do we get? Software Testing and Reliability Aditya P. Mathur 2003 82
BVA (contd. ) n In the previous examples we considered only numeric data. n BVA can be done on any type of data. n n For example, suppose that a program takes a string S and an integer X as inputs. The constraints on inputs are: n length(S)<=100 and a<=X<=b Can you derive the test cases using BVA? Software Testing and Reliability Aditya P. Mathur 2003 83
BVA Applied to Output Variables n n n Just as we applied BVA to input data, we can apply it to output data. Doing so gives us equivalence classes for the output domain. We then try to find test inputs that will cover each output equivalence class. Software Testing and Reliability Aditya P. Mathur 2003 84
Finite State Machines (FSMs) n n A state machine is an abstract representation of actions taken by a program or anything else that functions! It is specified as a quintuple: n A: a finite input alphabet n Q: a finite set of states n q 0: initial state which is a member of Q. Software Testing and Reliability Aditya P. Mathur 2003 85
FSMs (contd. ) n n n T: state transitions which is a mapping Q x A--> Q F: A finite set of final states, F is a subset of Q. Example: Here is a finite state machine that recognizes integers ending with a carriage return character. n n n A={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, CR} Q={q 0, q 1, q 2} q 0: initial state Software Testing and Reliability Aditya P. Mathur 2003 86
FSMs (contd. ) n n n T: {((q 0, d), q 1), (q 1, CR), q 2)} F: {q 2} A state diagram is an easier to understand specification of a state machine. For the above machine, the state diagram appears on the next slide. Software Testing and Reliability Aditya P. Mathur 2003 87
State diagram d q 0 d q 1 CR States indicated by circles. q 2 State transitions indicated by labeled arrows from one state the another (which could be the same). Each label must be from the alphabet. It is also known as an event. Final state indicated by concentric circles. d: denotes a digit Software Testing and Reliability Aditya P. Mathur 2003 88
State Diagram-Actions x/y: x is an element of the alphabet and y is an action. d/add 10*d to i d/set i to d q 0 q 2 q 1 CR/output i i is initialized to d when the machine moves from state q 0 to q 1. i is incremented by 10*d when the machine moves from q 1 to q 1. The current value of i is output when a CR is encountered. Can you describe what this machine computes? Can you construct a regular expression that describes all strings recognized by this state machine? Software Testing and Reliability Aditya P. Mathur 2003 89
State Machine: Languages n n Each state machine recognizes a language. The language recognized by a state machine is the set S of all strings such that: n when any string s in S is input to the state machine the machine goes through a sequence of transitions and ends up in the final state after having scanned all elements of s. Software Testing and Reliability Aditya P. Mathur 2003 90
State diagram-Errors d/add 10*d to I d/set I to d q 0 CR/output I CR reset q 2 q 1 /ou tpu t er ror q 4 has been added to the set of states. It represents an error state. Notice that reset is a new member added to the alphabet. Software Testing and Reliability Aditya P. Mathur 2003 91
State Diagram-Code n A state diagram can be transformed into a program using case analysis. Let us look at a C program fragment that embodies logic represented by the previous state diagram. n There is one function for each action. n digit is assumed to be provided by the lexical analyzer. Software Testing and Reliability Aditya P. Mathur 2003 92
Program for “integer” state machine /* state is global, with values q 0, q 1, q 2. i is also global. */ void event_digit() { switch (state) case q 0: i=digit; state=q 1; break; case q 1: i=i+10*digit; /* Add state=q 1; break; /* perform action. */ /* set next state. */ /* event digit is done. */ the next digit. */ /*…complete the program. */ Software Testing and Reliability Aditya P. Mathur 2003 93
Checking State Diagrams n Unreachable state: One that cannot be reached from q 0 using any sequence of transitions. n Dead state: One that cannot be left once it is reached. Software Testing and Reliability Aditya P. Mathur 2003 94
Test Requirements n n n Every state must be reached at least once, Obtain 100% state coverage. Every transition must be exercised at least once. Obtain 100% transition coverage. The textbook talks about duplicate transitions. No transitions are duplicate if the state machine definition we have given is used. Software Testing and Reliability Aditya P. Mathur 2003 95
Example: Test Requirements n For the “integer” state machine: n state machine transitions: n n n event event digit in state q 0 CR in state q 0 digit in state q 1 CR in state q 1 reset in state q 4 Software Testing and Reliability Aditya P. Mathur 2003 96
More testing of state machines? n n Yes, it is possible! When we learn about path coverage we will discuss how more test requirements can be derived from a state diagram. Software Testing and Reliability Aditya P. Mathur 2003 97
Test Specifications n n As before, test specifications are derived from test requirements. In the absence of dead states, all states and transitions might be reachable by one test. n It is advisable not to test the entire machine with one test case. n Develop test specifications for our “integer” state machine. Software Testing and Reliability Aditya P. Mathur 2003 98
Decision Tables n n Requirements of certain programs are specified by decision tables. A decision table is useful when specifying complex decision logic Software Testing and Reliability Aditya P. Mathur 2003 99
Decision Tables n n A decision table has two parts: n condition part n action part The two together specify under what condition will an action be performed. Software Testing and Reliability Aditya P. Mathur 2003 100
Decision Table-Nomenclature n n n n C: denotes a condition A: denotes an action Y: denotes true N: denotes false X: denotes action to be taken. Blank in condition: denotes “don’t care” Blank in action: denotes “do not take the action” Software Testing and Reliability Aditya P. Mathur 2003 101
Bank Example n Consider a bank software responsible for debiting from an account. The relevant conditions and actions are: n C 1: The account number is correct n C 2: The signature matches n C 3: There is enough money in the account n A 1: Give money n A 2: Give statement indicating insufficient funds n A 3: Call vigilance to check for fraud! Software Testing and Reliability Aditya P. Mathur 2003 102
Decision tables Software Testing and Reliability Aditya P. Mathur 2003 103
Example (contd. ) n n n A 1 is to be performed when C 1, C 2, and C 3 are true. A 2 is to be performed when C 1 is true and C 2 and C 3 are false or when C 1 and C 2 are true and C 3 is false. A 3 is to be performed when C 2 and C 3 are false. Software Testing and Reliability Aditya P. Mathur 2003 104
Default Rules n Are all possible combinations of conditions covered? n No! Which ones are not covered? n We need a default action for the uncovered combinations. A default action could be an error report or a reset. Software Testing and Reliability Aditya P. Mathur 2003 105
Example-Test Requirements n n n Each column is a rule and corresponds to at least one test requirement. If there are n columns then there at least n test requirements. What is the maximum number of test requirements? Software Testing and Reliability Aditya P. Mathur 2003 106
Example-Test Specifications n n n For each test requirement find a set of input values of variables such that the selected rule is satisfied. When this test is input to the program the output must correspond to the action specified in the decision table. Should the testing depend on the order in which the conditions are evaluated? Software Testing and Reliability Aditya P. Mathur 2003 107
Summary n Specifications, pre-conditions, and post-conditions. n Clues, test requirements, and test specifications. n Clues from code. n Test requirements catalog. n Equivalence partitioning and boundary value analysis. n Testing state machines. Software Testing and Reliability Aditya P. Mathur 2003 108
Summary-continued n Finite state machine n State diagram n Events and actions n Unreachable and dead states n Test requirements and specifications for state machines n Decision tables, rules, actions Software Testing and Reliability Aditya P. Mathur 2003 109
- Slides: 109