Bird
0
0

You want to check if a list items contains exactly 3 elements using pytest assert. Which is the best assert statement?

hard🚀 Application Q15 of 15
PyTest - Writing Assertions
You want to check if a list items contains exactly 3 elements using pytest assert. Which is the best assert statement?
Aassert len(items) == 3, "List does not have 3 elements"
Bassert items == 3, "List length check failed"
Cassert items.length == 3, "Wrong length"
Dassert len(items) = 3, "Length must be 3"
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to get list length in Python

    Use the built-in function len() to get the number of elements in a list.
  2. Step 2: Check correct assert syntax for length comparison

    Compare len(items) with 3 using '==', and provide a clear message after a comma.
  3. Final Answer:

    assert len(items) == 3, "List does not have 3 elements" -> Option A
  4. Quick Check:

    len(list) == number [OK]
Quick Trick: Use len() and '==' to check list size [OK]
Common Mistakes:
MISTAKES
  • Comparing list directly to number
  • Using JavaScript style .length property
  • Using assignment '=' instead of '=='

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes