Bird
0
0

Why is it important to use yield in a pytest fixture for browser setup and teardown instead of just return?

hard📝 Conceptual Q10 of 15
Selenium Python - Test Framework Integration (pytest)
Why is it important to use yield in a pytest fixture for browser setup and teardown instead of just return?
AYield allows separating setup and teardown code, running teardown after the test completes.
BReturn automatically closes the browser after the test.
CYield causes the fixture to run multiple times per test.
DReturn is not supported in pytest fixtures.
Step-by-Step Solution
Solution:
  1. Step 1: Understand difference between yield and return in fixtures

    Yield pauses the fixture to run the test, then resumes to run teardown code.
  2. Step 2: Recognize why teardown must run after test

    Teardown code like driver.quit() must run after the test finishes, which yield enables.
  3. Final Answer:

    Yield allows separating setup and teardown code, running teardown after the test completes. -> Option A
  4. Quick Check:

    Yield separates setup and teardown phases [OK]
Quick Trick: Yield splits setup and teardown in fixtures [OK]
Common Mistakes:
  • Thinking return runs teardown automatically
  • Believing yield runs fixture multiple times
  • Assuming return is unsupported in fixtures

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes