Лекция 6: Test-Case Design 1 What is a Test? Test Oracle: To verify the execution is correct, we need to compare the actual outcome with the expected outcome. Test oracle is a tool that can return the expected outcome for a given input vector. An executable specification of a program can be used as a test oracle for that program. 2 White box testing Syntactic abstraction of source code. Test cases are determined from the implementation of the software. White box testing: flow graphs ◦ control flow testing ◦ data flow testing. Purpose of white box test case generation: Coverage of the flow graph in accordance with one or more test criteria. 3 Control Flow Testing Control flow testing uses the control structure of a program to develop the test cases for the program. The test cases are developed to sufficiently cover the whole control structure of the program. The control structure of a program can be represented by the control flow graph of the program. The control flow graph G = (N, E) of a program consists of a set of nodes N and a set of edge E. 4 Control Flow Graph Each node represents a set of program statements: ◦ A decision node contains a conditional statement that creates 2 or more control branches (e.g. if or switch statements). ◦ A merge node usually does not contain any statement and is used to represent a program point where multiple control branches merge. ◦ A statement node contains a sequence of statements. The control must enter from the first statement and exit from the last statement. There is an edge from node n1to node n2 if the control may flow from the last statement in n1 to the first statement in n2. 5 Control Flow Graph: An Example 6 Test Cases A test case is a complete path from the entry node to the exit node of a control flow graph. A test coverage criterion measures the extent to which a set of test cases covers a program: ◦ Statement coverage (SC) – node coverage ◦ Decision coverage (DC) – edge coverage ◦ Condition coverage (CC) ◦ Decision/condition coverage (D/CC) ◦ Multiple condition coverage (MCC) ◦ Path coverage (PC) 7 Test Case Test case: {𝑖 ∈ 𝐷, 𝑜 ∈ 𝑂} Test suite: set of test cases 8 Statement Coverage Every statement in the program has been executed at least once. 9 Decision Coverage Every statement in the program has been executed at least once, and every decision in the program has taken all possible outcomes at least once. 10 Example public void foo(int a, int b, int x) { if (a>1 && b==0) { x=x/a; } if (a==2 || x>1) { x=x+1; } } Decision coverage: 1.ace and abd 2.acd and abe А = 3, В = 0, X = 3 А = 2, В = 1, и X = 1 11 Condition Coverage Every statement in the program has been executed at least once, and every condition in each decision has taken all possible outcomes at least once. 12 Example Condition Coverage four conditions: A>1, B=0, A=2, and X>1 point a: A>1, A<=1, B=0, and B<>0 point b: A=2, A<>2, X>1, and X<=1 Test cases: ◦ ace: A=2, B=0, X=4 ◦ adb: A=1, B=1, X=1 13 Decision/Condition Coverage Every statement in the program has been executed at least once, every decision in the program has taken all possible outcomes at least once, and every condition in each decision has taken all possible outcomes at least once. 14 Multiple Condition Coverage Every statement in the program has been executed at least once, all possible combination of condition outcomes in each decision has been invoked at least once. There are 2n combinations of condition outcomes in a decision with n conditions. 15 Example Test cases must cover eight combinations: Test-case input values, and the combinations they cover, are as follows: 16 Path Coverage Every complete path in the program has been executed at least once. A loop usually has an infinite number of complete paths. 17 White-box: loop testing Statement and branch coverage are not sufficient Single loop strategy: ◦ Zero iterations ◦ One iteration ◦ Two iterations ◦ Typical number of iterations ◦ n-1, n, and n+1 iterations (n maximum number of allowable ◦ iterations) Nested loop strategy: ◦ Single loop strategy often intractable ◦ Select minimum values for outer loop(s) ◦ Treat inner loop as a single loop ◦ Work ‘outwards’ and choose typical values for inner loops 18 Path Selection It is better to take many simple paths than a few complicated ones. There is no harm in taking paths that will exercise the same code more than once. Select paths as small variations of previous paths. Try to change one thing in each path at a time. 19 Example 20