Bird
0
0

You want to write a unit test for a function that returns the maximum number from a list. Which test case below best checks the function handles an empty list correctly?

hard📝 Application Q15 of 15
Testing Fundamentals - Testing Types and Levels
You want to write a unit test for a function that returns the maximum number from a list. Which test case below best checks the function handles an empty list correctly?
Aassert max_number([-1, -2]) == -1
Bassert max_number([1, 2, 3]) == 3
Cassert max_number([0]) == 0
Dassert max_number([]) == None
Step-by-Step Solution
Solution:
  1. Step 1: Understand the edge case

    An empty list has no maximum value, so the function should handle this gracefully, often by returning None or raising an error.
  2. Step 2: Evaluate test cases

    assert max_number([]) == None tests the empty list case expecting None, which is appropriate. Other options test normal lists, not empty.
  3. Final Answer:

    assert max_number([]) == None -> Option D
  4. Quick Check:

    Empty list test expects None [OK]
Quick Trick: Test empty inputs with None or error expected [OK]
Common Mistakes:
  • Not testing empty list edge case
  • Using normal list tests for empty case
  • Assuming empty list returns zero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Testing Fundamentals Quizzes