Bird
0
0

You want to test a function that returns a list of even numbers from 1 to 5. Which assert statement with a message best helps debug if the test fails?

hard🚀 Application Q15 of 15
PyTest - Writing Assertions
You want to test a function that returns a list of even numbers from 1 to 5. Which assert statement with a message best helps debug if the test fails?
def get_evens():
    return [2, 4]

def test_evens():
    result = get_evens()
    # Choose the best assert with message
Aassert result == [2, 4], "Check the function"
Bassert result == [2, 4], "Test failed"
Cassert result == [2, 4], f"Expected [2, 4], got {result}"
Dassert result == [2, 4], f"Result is {result}"
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of assert messages

    Good messages explain exactly what was expected and what was received to help debugging.
  2. Step 2: Compare options for clarity

    assert result == [2, 4], f"Expected [2, 4], got {result}" uses an f-string to show both expected and actual results clearly, aiding quick diagnosis.
  3. Final Answer:

    assert result == [2, 4], f"Expected [2, 4], got {result}" -> Option C
  4. Quick Check:

    Clear message shows expected vs actual [OK]
Quick Trick: Show expected and actual values in assert message [OK]
Common Mistakes:
MISTAKES
  • Using vague messages without details
  • Not showing actual result in message
  • Ignoring helpful f-string formatting

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes