Bird
0
0

Why is request.addfinalizer preferred over using yield in some pytest fixtures for cleanup?

hard🧠 Conceptual Q10 of 15
PyTest - Fixtures
Why is request.addfinalizer preferred over using yield in some pytest fixtures for cleanup?
ABecause yield fixtures cannot return values
BBecause addfinalizer allows multiple cleanup functions to be registered easily
CBecause addfinalizer runs cleanup before the test starts
DBecause yield fixtures do not support parameterization
Step-by-Step Solution
Solution:
  1. Step 1: Compare addfinalizer and yield

    Yield fixtures support one teardown block, while addfinalizer allows multiple cleanup functions to be added.
  2. Step 2: Understand benefits of addfinalizer

    Using addfinalizer makes it easier to register multiple independent cleanup actions in the same fixture.
  3. Final Answer:

    Because addfinalizer allows multiple cleanup functions to be registered easily -> Option B
  4. Quick Check:

    addfinalizer supports multiple cleanups; yield supports one [OK]
Quick Trick: Addfinalizer supports multiple cleanups; yield supports one [OK]
Common Mistakes:
MISTAKES
  • Thinking yield cannot return values
  • Believing addfinalizer runs before test
  • Confusing parameterization support

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes