Bird
0
0

Identify the error in the following pytest code:

medium📝 Debug Q6 of 15
PyTest - Fixtures
Identify the error in the following pytest code:
import pytest

@pytest.fixture
def data():
    return [1, 2, 3]

def test_sum():
    result = data + [4]
    assert sum(result) == 10
AThe fixture 'data' should return a tuple, not a list
BThe fixture decorator is missing
CThe assertion is incorrect; sum should be 9
DThe fixture 'data' is not passed as a function argument to the test
Step-by-Step Solution
Solution:
  1. Step 1: Check fixture usage in test

    The fixture 'data' is defined but not passed as an argument to 'test_sum', so 'data' is undefined in the test.
  2. Step 2: Identify the error caused

    Trying to add 'data' (a function) to a list causes a TypeError because 'data' is not called or injected.
  3. Final Answer:

    The fixture 'data' is not passed as a function argument to the test -> Option D
  4. Quick Check:

    Fixture must be function argument = error if missing [OK]
Quick Trick: Always pass fixture as test function argument [OK]
Common Mistakes:
MISTAKES
  • Using fixture name without passing as argument
  • Expecting fixture to run automatically without argument
  • Misunderstanding fixture return types

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes