We use conftest.py to share setup code called fixtures across many test files. This saves time and keeps tests clean.
Conftest for shared fixtures in Selenium Python
Introduction
Syntax
Selenium Python
import pytest @pytest.fixture def fixture_name(): # setup code yield resource # teardown code
Fixtures are functions marked with @pytest.fixture.
Use yield to return the resource and then run cleanup after tests.
Examples
Selenium Python
import pytest @pytest.fixture def browser(): from selenium import webdriver driver = webdriver.Chrome() yield driver driver.quit()
Selenium Python
import pytest @pytest.fixture(scope='session') def browser(): from selenium import webdriver driver = webdriver.Firefox() yield driver driver.quit()
Sample Program
This test file uses the shared browser fixture from conftest.py style setup. It opens Chrome for each test and closes it after.
Selenium Python
import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit() def test_google_title(browser): browser.get('https://www.google.com') assert 'Google' in browser.title def test_python_org_title(browser): browser.get('https://www.python.org') assert 'Python' in browser.title
Important Notes
Put conftest.py in the root test folder to share fixtures automatically.
Fixtures can have scopes: function (default), class, module, session.
Use fixtures to keep tests simple and avoid repeating setup code.
Summary
conftest.py holds shared fixtures for setup and cleanup.
Fixtures help reuse browser or data setup across tests.
Use @pytest.fixture and yield to manage resources.
Practice
1. What is the main purpose of a
conftest.py file in Selenium Python testing?easy
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]
Hint: Remember: conftest.py shares setup code across tests [OK]
Common Mistakes:
- Confusing conftest.py with test case files
- Thinking conftest.py stores test data
- Using conftest.py to write test assertions
2. Which of the following is the correct way to declare a fixture in
conftest.py using pytest?easy
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]
Hint: Always prefix fixture functions with @pytest.fixture [OK]
Common Mistakes:
- Omitting @pytest.fixture decorator
- Using wrong decorator name like @fixture
- Not using yield for setup/teardown
3. Given this fixture in
conftest.py:
@pytest.fixture
def browser():
driver = webdriver.Chrome()
yield driver
driver.quit()
What happens when a test uses this fixture?medium
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]
Hint: yield splits setup and teardown in fixtures [OK]
Common Mistakes:
- Thinking yield closes browser immediately
- Forgetting driver.quit() after yield
- Assuming browser stays open forever
4. You wrote this fixture in
conftest.py:
@pytest.fixture
def browser():
driver = webdriver.Chrome()
return driver
driver.quit()
What is the problem with this fixture?medium
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]
Hint: Use yield, not return, to ensure cleanup runs [OK]
Common Mistakes:
- Placing driver.quit() after return
- Using return instead of yield for fixtures
- Expecting cleanup code after return to run
5. You want to share a browser fixture in
conftest.py that opens Chrome once per test session and closes it after all tests finish. Which fixture scope and code is correct?hard
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]
Hint: Use scope='session' and yield for one browser per session [OK]
Common Mistakes:
- Using scope='function' for session-wide browser
- Calling driver.quit() before yield
- Yielding after driver.quit()
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
