Check your understanding of the previous reading: counting executed lines, calculating code coverage, and telling valid tests from thorough ones.

Question 1

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.

  • Not quite. Every program executes at least its import, its def line and its test, and the test calls the function, which runs part of its body as well.
  • Not quite. Every program executes at least its import, its def line and its test, and the test calls the function, which runs part of its body as well.
  • Not quite. Every program executes at least its import, its def line and its test, and the test calls the function, which runs part of its body as well.
  • Not quite. Every program executes at least its import, its def line and its test, and the test calls the function, which runs part of its body as well.
  • Not quite. Count the lines that run before the test, too: the import and the def line are executed when the program starts, and the assert_equal line is executed when the test runs.
  • Not quite. Count the lines that run before the test, too: the import and the def line are executed when the program starts, and the assert_equal line is executed when the test runs.
  • Correct. The program has 7 actual lines. The import, the 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.
  • Not quite. The program has 7 actual lines, but not all of them run: the function returns before it reaches return "Non-positive".
  • Not quite. The blank line is not an actual line of code, so the program only has 7 lines, and one of them never runs.

Question 2

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.

  • Not quite. The import, the def line and the test always run, so coverage cannot be 0%.
  • Not quite. Only the body of the if is skipped; the rest of the program runs.
  • Not quite. Count every line that runs, including the import, the def line and the test itself, not only the lines inside the function.
  • Correct. The program has 8 actual lines, counting 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.
  • Not quite. The test only tries a negative number, so the if body (message = "Positive" and return message) never runs.

Question 3

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.

  • Not required. The reading never asks for a minimum number of tests: what matters is how they behave on correct and incorrect programs.
  • Correct. To be thorough, at least one test must fail on every incorrect implementation, and with no tests at all, nothing can fail.
  • Not required. The reading says valid and thorough "are not about code coverage, but instead about the quality of your tests".
  • Not required as stated. The tests must pass on every correct program, but on an incorrect program at least one of them has to fail, so they do not simply "all pass".
  • Correct. That is what makes a set of tests valid.
  • Correct. That is what makes a set of tests thorough.

Question 4

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.

  • Valid. "4" is a string representing an even integer, so a correct is_even returns True.
  • Not valid. 4 is even, so every correct implementation returns True and this test fails on it.
  • Not valid. 3 is odd, so every correct implementation returns False and this test fails on it.
  • Valid. "3" is a string representing an odd integer, so a correct is_even returns False.
  • Not valid. is_even consumes a string, and 3 is an integer: this tests an invalid input, which a correct implementation may well crash on.
  • Not valid. is_even consumes a string, and 4 is an integer: this tests an invalid input, which a correct implementation may well crash on.

Question 5

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.

  • Not quite. This test passes on can_drive1, can_drive2 and can_drive3, but catches can_drive4, which returns False because 16 is not greater than 16.
  • Not quite. This test catches can_drive2 and can_drive3, which both return True; it passes on only two of the imposters.
  • Not quite. This test catches can_drive1 and can_drive3, which both return True; it passes on only two of the imposters.
  • Correct. All four imposters return False for someone who is 15 and has no license, so this test passes on every one of them and catches none.