Loading...

Statement Testing and Statement Coverage

Statement Testing

 focuses on verifying that the individual statements in a program’s code are executed at least once during testing. It aims to identify and remove defects within the statements by ensuring thorough coverage.

Key Concepts

  1. Statement Coverage:
  • Measures the percentage of executable statements tested by the test cases.

Formula:
Statement Coverage (%) = (Number of Executed Statements / Total Executable Statements) × 100

2. Objective:

  • Ensure every line of code is executed at least once.
  • Detect defects that may exist in specific statements.

Benefits of 100% Statement Coverage

  • Defect Detection: Ensures that every statement is executed, helping to uncover defects.
  • Code Confidence: Confirms that all lines of code are reachable and functional.

Limitations

  1. Data-Dependent Defects:
  • For instance, a division-by-zero error will not be caught unless a specific input triggers the defect.

2. Decision Logic Coverage:

  • Statement coverage does not ensure all branches in decision-making structures (e.g., ifelseswitch) are tested.
  • Example: In an if-else a statement, achieving 100% statement coverage may test only one path (e.g., if), leaving the else untested.

Example

Consider the following code snippet:

if (x > 0):  
print("Positive number")
else:
print("Non-positive number")
print("End of program")
  • Test Case 1: Input x = 5
  • Output: Positive numberEnd of program
  • It covers the if statement and the final print statement.
  • Test Case 2: Input x = -3
  • Output: Non-positive numberEnd of program
  • It covers the else statement and the final print statement.

100% statement coverage requires both test cases since all lines of code are executed at least once.

Takeaway: While 100% statement coverage is a useful goal, testers should also focus on other coverage types (e.g., branch coverage) to ensure the entire decision logic is thoroughly tested.

Here are some ISTQB-style MCQs related to Statement Testing and Statement Coverage:

1. What does statement testing focus on?

a) Ensuring all branches in the code are tested
b) Ensuring all lines of code are executed at least once
c) Ensuring all decision outcomes are tested
d) Ensuring all test cases are valid

Answer: b) Ensuring all lines of code are executed at least once

2. Which of the following is true about 100% statement coverage?

a) It guarantees the absence of defects in the software
b) It ensures all branches in decision-making are tested
c) It ensures every executable statement in the code is tested at least once
d) It ensures all combinations of inputs are tested

Answer: c) It ensures every executable statement in the code is tested at least once

3. Which metric is used to measure statement coverage?

a) (Number of executed branches / Total branches) × 100
b) (Number of executed statements / Total executable statements) × 100
c) (Number of executed decisions / Total decisions) × 100
d) (Number of test cases / Total test cases) × 100

Answer: b) (Number of executed statements / Total executable statements) × 100

4. Which of the following is NOT detected by 100% statement coverage?

a) Syntax errors in unexecuted code
b) Defects in executed statements
c) Dead code that is unreachable
d) Data-dependent errors, such as division by zero

Answer: d) Data-dependent errors, such as division by zero

5. Statement coverage is achieved by executing which of the following?

a) All paths in the program
b) All statements in the program
c) All conditions in the program
d) All branches in the program

Answer: b) All statements in the program

6. What is the main limitation of 100% statement coverage?

a) It is impossible to achieve in most real-world applications
b) It does not test decision logic or all possible outcomes
c) It requires testing every possible input value
d) It only applies to non-executable statements

Answer: b) It does not test decision logic or all possible outcomes

7. In the following code snippet, which test case achieves 100% statement coverage?

if x > 0:  
print("Positive")
else:
print("Non-positive")
print("Done")

a) Test case: x = 1
b) Test case: x = -1
c) Test case: x = 0
d) Test cases: x = 1 and x = -1

Answer: d) Test cases: x = 1 and x = -1

8. Which coverage type is weaker than statement coverage?

a) Branch coverage
b) Condition coverage
c) Decision coverage
d) None of the above

Answer: d) None of the above