Bird
0
0

Examine the following pytest fixture code for browser setup and teardown. What is the issue here?

medium📝 Debug Q6 of 15
Selenium Python - Test Framework Integration (pytest)
Examine the following pytest fixture code for browser setup and teardown. What is the issue here?
@pytest.fixture
def browser():
    driver = webdriver.Chrome()
    yield driver
    driver.quit()
    driver = None
AThe fixture is missing a return statement instead of yield.
BThe yield statement should be placed after driver.quit() to ensure proper setup.
Cdriver.quit() should be called before yield to close the browser before the test runs.
DSetting driver to None after quitting is unnecessary and has no effect on teardown.
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the fixture flow

    The fixture creates a WebDriver instance, yields it for the test, then calls driver.quit() to close the browser.
  2. Step 2: Check the line after driver.quit()

    Setting driver = None after quitting the driver is redundant because the driver object is already closed and will be garbage collected.
  3. Final Answer:

    Setting driver to None after quitting is unnecessary and has no effect on teardown. -> Option D
  4. Quick Check:

    Yield must come before quit; setting driver to None is harmless but redundant. [OK]
Quick Trick: Yield before quit; no need to set driver to None after quitting [OK]
Common Mistakes:
  • Placing yield after driver.quit()
  • Calling driver.quit() before yield
  • Confusing yield with return in fixtures

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Selenium Python Quizzes