Bird
0
0

What is wrong with this pytest assertion?

medium📝 Debug Q6 of 15
PyTest - Writing Assertions
What is wrong with this pytest assertion?
def test_total():
    total = 4 + 4
    assert total == 8 == True
AThe assert statement is missing a message
BThe variable 'total' is not defined
CThe assertion uses a chained comparison incorrectly
DThere is no error; the assertion is correct
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the assertion

    The expression total == 8 == True is a chained comparison.
  2. Step 2: Understand chained comparisons

    It checks if total == 8 and 8 == True. Since 8 == True is false, the whole assertion fails unexpectedly.
  3. Step 3: Correct usage

    The assertion should be assert total == 8 without chaining.
  4. Final Answer:

    The assertion uses a chained comparison incorrectly -> Option C
  5. Quick Check:

    Chained comparisons can cause logic errors [OK]
Quick Trick: Avoid chaining comparisons in assert statements [OK]
Common Mistakes:
MISTAKES
  • Using chained comparisons that evaluate unexpectedly
  • Assuming chained comparisons always work as intended
  • Confusing equality with boolean checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes