Bird
0
0

You want to verify that a function fetch_data() returns a list containing exactly 3 elements. Which pytest assert statement is most appropriate?

hard🚀 Application Q8 of 15
PyTest - Writing Assertions
You want to verify that a function fetch_data() returns a list containing exactly 3 elements. Which pytest assert statement is most appropriate?
def test_fetch_data():
    data = fetch_data()
    # Choose the correct assert
Aassert data.length == 3
Bassert data == 3
Cassert len(data) == 3
Dassert data.count == 3
Step-by-Step Solution
Solution:
  1. Step 1: Understand the data type

    data is a list, so use len() to get its length.
  2. Step 2: Check correct syntax

    assert len(data) == 3 correctly asserts the list length.
  3. Step 3: Identify incorrect options

    data == 3 compares list to int, data.length and data.count are invalid in Python lists.
  4. Final Answer:

    assert len(data) == 3 -> Option C
  5. Quick Check:

    Use len() to check list length [OK]
Quick Trick: Use len() to assert list length in pytest [OK]
Common Mistakes:
MISTAKES
  • Comparing list directly to an integer
  • Using non-existent attributes like length or count
  • Forgetting to use len() for length checks

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes