Bird
0
0

Consider this fixture in conftest.py:

medium📝 Debug Q6 of 15
PyTest - Fixtures
Consider this fixture in conftest.py:
@pytest.fixture
def items():
    return [1, 2, 3]

and the test:
def test_append(items):
    items.append(4)
    assert items == [1, 2, 3, 4]

What issue might arise when running multiple tests using this fixture?
APytest will raise an error about modifying fixtures.
BThe list is shared and mutated across tests, causing side effects.
CThe fixture returns a new list each time, so no issues occur.
DThe test will always fail because append is not allowed.
Step-by-Step Solution
Solution:
  1. Step 1: Understand fixture behavior

    By default, fixtures have function scope and return a new list each time.
  2. Step 2: Mutation in test

    The test mutates the list by appending an element.
  3. Step 3: Potential problem

    If the fixture is changed to a broader scope (e.g., module or session), the list would be shared and mutations persist across tests, causing side effects.
  4. Step 4: Conclusion

    Mutating shared fixtures can lead to flaky tests.
  5. Final Answer:

    The fixture returns a new list each time, so no issues occur. -> Option C
  6. Quick Check:

    Function-scoped fixture returns fresh data each test [OK]
Quick Trick: Avoid mutating fixtures with broader scopes [OK]
Common Mistakes:
MISTAKES
  • Assuming fixture always returns fresh data
  • Ignoring side effects of mutable objects
  • Expecting pytest to prevent mutations automatically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes