80 likes | 162 Views
CS4311 Spring 2011 Unit Testing Dr. Guoqiang Hu Department of Computer Science UTEP. Statement Coverage Every statement gets executed at least once Every node in the CFG gets visited at least once Statement coverage is the most basic (weakest) coverage. 1. 6. 1. 6.
E N D
CS4311 Spring 2011 Unit Testing Dr. GuoqiangHuDepartment of Computer ScienceUTEP
Statement Coverage • Every statement gets executed at least once • Every node in the CFG gets visited at least once Statement coverage is the most basic (weakest) coverage. 1 6 1 6 Groups of 2 in 3 minutes: The number of paths needed for statement coverage? 2 7 2 7 3 8 3 8 4 9 4 9 4 1 5 5
Branch Coverage Also known as decision/edge coverage, stronger than statement coverage: • Every decision is made true and false at least once each • Every edge in a CFG of the program gets traversed at least once 1 6 1 6 Groups of 2 in 2 minutes: The number of paths needed for branch coverage? 2 7 2 7 3 8 3 8 4 9 4 9 5 2 5 5
Condition Coverage Every condition in a decision (a boolean expression) is made true and false by every possible combination. For example: (x and y) • x = true, y = true • x= false, y= true • x = true, y= false • x = false, y = false Condition coverage is not necessarily stronger than branch coverage: • There are pathological cases where you can achieve condition coverage but not branch coverage • However, under most circumstances, achieving condition coverage achieves branch coverage One way to determine the number of paths is to break the compound conditional into atomic conditionals when writing CFG
Path Coverage • A path is a sequence of statements from beginning to end • A path is sequence of branches from beginning to end • A path is a sequence of edges from beginning to end Every distinct path through code is executed at least once 1 6 1 6 Groups of 2 in 2 minutes: The number of paths needed for path coverage? 2 7 2 7 3 8 3 8 4 9 4 9 5 16 5 5
Path Coverage: Exercise 1,2,3 1. read (x) 2. read (z) 3. if x 0 then begin 4. y x * z; 5. x z end 6. else print ‘Invalid’ 7. if y > 1 then 8. print y 9. else print ‘Invalid’ Test Paths: 4 1, 2, 3, 4, 5, J1, 7, 8, J2 1, 2, 3, 4, 5, J1, 7, 9, J2 1, 2, 3, 6, J1, 7, 8, J2, 1, 2, 3, 6, J1, 7, 9, J2 Groups of 2 in 2 minutes: Find the number of paths, and the paths, needed for path coverage 4,5 6 Join1 7 8 9 Join2 Normally, it is not possible to calculate the total number of paths.
Path Coverage: Linearly Independent Paths A linearly independent path is an end-to-end path that introduces at least one new set of process statements or a new condition. Consider the following encoding: • Label each edge in the CFG with a positive integer • Describe a path using a vector where each element of the vector is the number of times the edge at that index is traversed For example: The path ABD (or, p13) can be written as: [1, 0, 1, 0] A 1 2 Let’s further consider: • Paths can be linearly combined by operations like: addition, subtraction, or scalar multiplication of path vectors • All the path vectors form a vector space • In this vector space, a basis set of vectors can be found, such that: in this basis set, each member cannot be derived from the other members; but, all the rest of vectors in the space can be derived from this basis set B C 4 3 D