Check your understanding of the previous reading: counting executed lines, calculating code coverage, and telling valid tests from thorough ones.
When the program below is executed, how many actual lines of code will be executed?
from bakery import assert_equal
def coverage_example(x):
if x > 0:
message = "Positive"
return message
return "Non-positive"
assert_equal(coverage_example(5), "Positive")
Select one answer, then check.
def line and its test, and the test calls the function, which runs part of its body as well.def line and its test, and the test calls the function, which runs part of its body as well.def line and its test, and the test calls the function, which runs part of its body as well.def line and its test, and the test calls the function, which runs part of its body as well.def line are executed when the program starts, and the assert_equal line is executed when the test runs.def line are executed when the program starts, and the assert_equal line is executed when the test runs.def line and the assert_equal call run first; the call then runs the if line, and because 5 > 0 it runs message = "Positive" and return message. Only return "Non-positive" is never reached, so 6 lines are executed.return "Non-positive".When the program below is executed, what will the code coverage be?
from bakery import assert_equal
def coverage_example(x):
if x > 0:
message = "Positive"
return message
else:
return "Non-positive"
assert_equal(coverage_example(-5), "Non-positive")
Select one answer, then check.
def line and the test always run, so coverage cannot be 0%.if is skipped; the rest of the program runs.def line and the test itself, not only the lines inside the function.else:. The import, the def line and the test run, and the call with -5 runs the if line, the else: line and return "Non-positive". The two lines of the if body never run, so 6 of the 8 lines are executed: 75% coverage.if body (message = "Positive" and return message) never runs.Which statements MUST be true for a set of unit tests to be considered both valid and thorough, as described in the chapter?
Select every answer that applies, then check.
The function is_even consumes a string representing an integer and returns True if the integer is even, and False otherwise. Which of the following tests are valid unit tests for the is_even function?
Select every answer that applies, then check.
"4" is a string representing an even integer, so a correct is_even returns True.True and this test fails on it.False and this test fails on it."3" is a string representing an odd integer, so a correct is_even returns False.is_even consumes a string, and 3 is an integer: this tests an invalid input, which a correct implementation may well crash on.is_even consumes a string, and 4 is an integer: this tests an invalid input, which a correct implementation may well crash on.The can_drive function consumes two parameters: age (an integer) and has_license (a boolean). It returns True if the person is allowed to drive, and False otherwise. A person can drive if they are at least 16 years old and have a valid driver's license. Here are four broken implementations of the can_drive function, which determines if a person is allowed to drive based on their age and whether they have a valid driver's license.
def can_drive1(age, has_license):
return age >= 16
def can_drive2(age, has_license):
return has_license
def can_drive3(age, has_license):
return age >= 16 or has_license
def can_drive4(age, has_license):
return age > 16 and has_license
Which of the following unit tests would be considered the LEAST thorough? That is, which test would pass the most of these broken implementations (imposters) of the can_drive function?
Select one answer, then check.
can_drive1, can_drive2 and can_drive3, but catches can_drive4, which returns False because 16 is not greater than 16.can_drive2 and can_drive3, which both return True; it passes on only two of the imposters.can_drive1 and can_drive3, which both return True; it passes on only two of the imposters.False for someone who is 15 and has no license, so this test passes on every one of them and catches none.