Which of the following best describes statement coverage in software testing?
Think about what part of the code is actually run during testing.
Statement coverage checks if every line of code has been run at least once by the tests. It does not focus on decision outcomes or test case counts.
Given the following code and test cases, what is the statement coverage percentage?
def check_number(x):
if x > 0:
print("Positive")
else:
print("Non-positive")
# Test cases:
check_number(5)Count how many lines run when check_number(5) is called.
There are 4 executable statements: the if, the print("Positive"), the else, and print("Non-positive"). Only the if and print("Positive") run, so 2 out of 4 lines = 50% coverage.
Which assertion correctly verifies that a test suite achieves 100% statement coverage for the following function?
def is_even(n):
if n % 2 == 0:
return True
else:
return FalseFull coverage means all lines run at least once.
To confirm 100% statement coverage, the assertion must check that coverage equals 100%. Other options are incorrect because they allow partial or no coverage.
Consider this test code measuring statement coverage. It reports 80% coverage, but the tester expects 100%. What is the likely cause?
def foo(x):
if x > 0:
print("Positive")
else:
print("Zero or Negative")
# Test cases
foo(1)
# Coverage measured hereThink about which lines run when foo(1) is called.
Calling foo(1) only runs the if branch, so the else branch statements are never executed, causing less than 100% coverage.
Which option correctly describes how to integrate statement coverage measurement into an automated Python test suite using pytest and coverage.py?
Think about the standard commands to run coverage with pytest.
The correct way is to run tests under coverage using coverage run -m pytest and then generate a coverage report with coverage report. Option A is invalid because pytest alone does not support --coverage without plugins. Option A is incorrect because coverage is measured externally, not by assertions. Option A is wrong because reports require explicit commands.