Unit Tests: Deciding What Is Worth Testing · seed 1 · A4, ink-friendly. The answer key prints on its own page for grown-ups.
به انگلیسی

Tests that check a promise

Computing · Programming · ages 17-18
Name ______________________   Date ____________
  1. grade_label(score) accepts integer scores from 0 through 100. Which supplied score is a boundary of that allowed domain?

    • score = 50
    • score = 0
    • score = 70
  2. For the documented split_bill contract, which plan covers ordinary inputs, edges of its allowed domain and its specified rejected inputs?

    • Only ordinary valid calls, repeated with different totals
    • Ordinary calls, relevant boundaries and expected errors
    • Only rejected calls, repeated with different invalid inputs
  3. A unit-test suite can run its checks automatically without a person clicking through application screens.

    Circle one:   True   False

  4. A test asserts split_bill(18, 3) == 6. The function is changed from division to multiplication and now returns 54 for that call. What should this assertion do?

    • Pass because the function still returned a numeric result
    • Fail because 54 differs from the expected result 6
    • Pass because the expected value must follow the new code
  5. split_bill(45, 5) should return 9 but returns 8. Which failure message accurately identifies this broken promise?

    • Expected 9; got 8 for split_bill(45, 5)
    • Expected 8; got 9 for split_bill(45, 5)
    • Expected 9; got 8 for split_bill(40, 5)
  6. A test calculates its expected result by copying the function's implementation logic. What correctness risk does this create?

    • Matching computations rule out errors in the expected value
    • Both computations may share the same undetected mistake
    • Repeating the computation checks inputs never supplied
  7. shipping_weight([]) must return 0. Every test in the passing suite supplies a nonempty list. Users find that empty input crashes. Which coverage gap is established by these stated tests?

    • An empty-list test uses an incorrect expected result
    • An empty-list test checks only the result's data type
    • No test checks the required empty-list behaviour
  8. mean(values) is specified to raise ValueError for an empty list. check is a unittest.TestCase instance. Which test checks that specified error?

    • Use assertRaises(ValueError) around mean([])
    • Use assertRaises(IndexError) around mean([])
    • Assert that mean([]) returns zero rather than raising an exception
LightMySky · lightmysky.comW1-mt_tuIQecq5Le-s1

Answer key

For grown-ups. Fold this page away before handing over the rest.

Tests that check a promise W1-mt_tuIQecq5Le-s1

  1. score = 0 · Zero is the stated lower endpoint. Fifty and seventy lie inside the allowed interval.
  2. Ordinary calls, relevant boundaries and expected errors · This function's contract includes valid results, domain boundaries and rejected inputs, so the plan should address each.
  3. True · Unit tests encode calls and checks that the test runner can execute automatically.
  4. Fail because 54 differs from the expected result 6 · The change affects this test's exact input and promise. The observed result, 54, does not equal the independently expected 6.
  5. Expected 9; got 8 for split_bill(45, 5) · The message must name the actual call, the expected 9 and the observed 8 without swapping them.
  6. Both computations may share the same undetected mistake · Copied logic can make the expected and observed results agree on the same error. It does not necessarily make the test worthless.
  7. No test checks the required empty-list behaviour · The question supplies the actual coverage: all tested lists are nonempty. Therefore no test checks the required empty-list result.
  8. Use assertRaises(ValueError) around mean([]) · assertRaises(ValueError) expects the specified exception. The test passes when the call raises that matching error.
Worksheet · LightMySky