Bird
Raised Fist0

Which of the following is the correct syntax for a simple unit test assertion in Python using assert?

easy📝 Syntax Q12 of Q15
Testing Fundamentals - Testing Types and Levels
Which of the following is the correct syntax for a simple unit test assertion in Python using assert?
Aassert add(2, 3) == 5
Bassert add(2, 3) = 5
Cassert(add(2, 3) = 5)
Dassert add(2, 3) equals 5
Step-by-Step Solution
Solution:
  1. Step 1: Recall Python assert syntax

    Python uses assert expression where expression must be True for test to pass.
  2. Step 2: Check each option

    assert add(2, 3) == 5 uses correct syntax: assert add(2, 3) == 5. assert add(2, 3) = 5 uses single = which is assignment, invalid here. assert(add(2, 3) = 5) uses single = which is assignment, invalid syntax. assert add(2, 3) equals 5 uses invalid keyword 'equals'.
  3. Final Answer:

    assert add(2, 3) == 5 -> Option A
  4. Quick Check:

    assert with == for comparison [OK]
Quick Trick: Use 'assert expression' with '==' for comparison in Python [OK]
Common Mistakes:
MISTAKES
  • Using single '=' instead of '==' in assert
  • Writing assert with invalid keywords
  • Adding unnecessary parentheses in assert

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Testing Fundamentals Quizzes