Discover how one file can save you hours of repetitive test setup work!
Why Conftest for shared fixtures in Selenium Python? - Purpose & Use Cases
Imagine you are testing a website with many test files. Each test needs to open a browser, log in, and set up data. You write the same setup code in every test file.
This manual way is slow and tiring. If you change the setup, you must update every test file. It is easy to forget one place, causing errors and wasted time.
Using conftest.py for shared fixtures lets you write setup code once. All tests can use it automatically. This keeps tests clean, easy to update, and faster to write.
from selenium import webdriver def test_login(): driver = webdriver.Chrome() driver.get('url') # login steps # test steps driver.quit()
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_login(browser): browser.get('url') # login steps # test steps
It enables writing clean, reusable test setups that save time and reduce mistakes across many tests.
When testing an online store, you can use a shared fixture to open the browser and log in once. All tests for adding items, checking out, or searching use the same setup without repeating code.
Manual setup in every test file is slow and error-prone.
Conftest.py lets you share fixtures across tests easily.
This makes tests cleaner, faster to write, and easier to maintain.
Practice
conftest.py file in Selenium Python testing?Solution
Step 1: Understand the role of
Theconftest.pyconftest.pyfile is used to define fixtures that can be shared across multiple test files for setup and cleanup tasks.Step 2: Differentiate from other files
It does not store test data, locators, or test cases directly but provides reusable setup code like browser initialization.Final Answer:
To hold shared fixtures for setup and cleanup across multiple test files -> Option AQuick Check:
conftest.py = shared fixtures [OK]
- Confusing conftest.py with test case files
- Thinking conftest.py stores test data
- Using conftest.py to write test assertions
conftest.py using pytest?Solution
Step 1: Identify correct fixture decorator usage
pytest fixtures require the@pytest.fixturedecorator above the function to mark it as a fixture.Step 2: Check syntax correctness
@pytest.fixture def browser(): yield webdriver.Chrome() correctly uses@pytest.fixtureand a generator withyieldto provide the resource.Final Answer:
@pytest.fixture\ndef browser(): yield webdriver.Chrome() -> Option BQuick Check:
Use @pytest.fixture decorator [OK]
- Omitting @pytest.fixture decorator
- Using wrong decorator name like @fixture
- Not using yield for setup/teardown
conftest.py:
@pytest.fixture
def browser():
driver = webdriver.Chrome()
yield driver
driver.quit()
What happens when a test uses this fixture?Solution
Step 1: Understand yield in fixture
Theyieldstatement pauses the fixture, providing the driver to the test, then resumes after the test to run cleanup.Step 2: Analyze setup and teardown
Driver is created before yield (browser opens), anddriver.quit()runs after test finishes (browser closes).Final Answer:
The browser opens before the test and closes after the test finishes -> Option DQuick Check:
yield = setup before, cleanup after [OK]
- Thinking yield closes browser immediately
- Forgetting driver.quit() after yield
- Assuming browser stays open forever
conftest.py:
@pytest.fixture
def browser():
driver = webdriver.Chrome()
return driver
driver.quit()
What is the problem with this fixture?Solution
Step 1: Analyze return and driver.quit() placement
Sincereturnexits the function immediately,driver.quit()after it never runs.Step 2: Understand impact on browser lifecycle
Withoutdriver.quit(), the browser stays open after tests, causing resource leaks.Final Answer:
driver.quit() is never called, so browser stays open after tests -> Option AQuick Check:
return ends function before cleanup [OK]
- Placing driver.quit() after return
- Using return instead of yield for fixtures
- Expecting cleanup code after return to run
conftest.py that opens Chrome once per test session and closes it after all tests finish. Which fixture scope and code is correct?Solution
Step 1: Choose correct fixture scope for session-wide browser
Usingscope='session'makes the fixture run once per test session.Step 2: Verify setup and teardown order with yield
Driver is created beforeyield(browser opens), anddriver.quit()runs after all tests finish.Final Answer:
@pytest.fixture(scope='session')\ndef browser():\n driver = webdriver.Chrome()\n yield driver\n driver.quit() -> Option CQuick Check:
scope='session' + yield = one browser per session [OK]
- Using scope='function' for session-wide browser
- Calling driver.quit() before yield
- Yielding after driver.quit()
Ready to prove it?
Pass the test and this pattern moves unknown
