What is the purpose of a conftest.py file in pytest?
The conftest.py file is used to define shared fixtures and hooks that can be used across multiple test files without importing them explicitly.
Click to reveal answer
beginner
How do you define a fixture in conftest.py for Selenium WebDriver?
You define a function with the @pytest.fixture decorator that initializes the WebDriver, yields it for use in tests, and then quits the driver after tests finish.
Click to reveal answer
beginner
Why is it better to put Selenium WebDriver setup in conftest.py instead of each test file?
Putting WebDriver setup in conftest.py avoids repeating code, makes tests cleaner, and ensures consistent setup and teardown across all tests.
Click to reveal answer
intermediate
What does the scope parameter in a pytest fixture control?
The scope parameter controls how often the fixture is invoked. For example, scope='function' runs before each test, scope='session' runs once per test session.
Click to reveal answer
beginner
How can you use a fixture from conftest.py in your test function?
Simply add the fixture name as a parameter to your test function. Pytest will automatically find and inject the fixture from conftest.py.
Click to reveal answer
What is the main benefit of using conftest.py for fixtures?
ATo run tests in parallel automatically
BTo write test cases faster
CTo avoid using Selenium WebDriver
DTo share fixtures across multiple test files without imports
✗ Incorrect
The conftest.py file allows sharing fixtures across test files without explicit imports.
Which decorator is used to define a fixture in pytest?
A@pytest.test
B@pytest.fixture
C@pytest.setup
D@pytest.driver
✗ Incorrect
Fixtures in pytest are defined using the @pytest.fixture decorator.
If you want a Selenium WebDriver fixture to run once per test session, which scope should you use?
Afunction
Bmodule
Csession
Dclass
✗ Incorrect
Using scope='session' makes the fixture run once for the entire test session.
How do you ensure the WebDriver quits after a test using a fixture?
AUse yield in the fixture and call driver.quit() after yield
BCall driver.quit() inside the test
CDo nothing, it quits automatically
DUse a global variable
✗ Incorrect
Using yield in the fixture allows setup before yield and teardown (like driver.quit()) after yield.
How do you use a fixture named browser in your test function?
AAdd <code>browser</code> as a parameter to the test function
BImport browser and call it
CCall <code>pytest.browser()</code>
DUse <code>self.browser</code> inside the test
✗ Incorrect
Pytest injects fixtures by matching parameter names in test functions.
Explain how to create and use a Selenium WebDriver fixture in conftest.py.
Think about setup and teardown using yield.
You got /5 concepts.
Describe the benefits of using conftest.py for shared fixtures in Selenium testing.
Consider how shared setup helps in many tests.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a conftest.py file in Selenium Python testing?
easy
A. To hold shared fixtures for setup and cleanup across multiple test files
B. To store test data in JSON format
C. To define Selenium WebDriver locators
D. To write test cases directly
Solution
Step 1: Understand the role of conftest.py
The conftest.py file 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 A
Quick 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
A. def browser(): yield webdriver.Chrome()
B. @pytest.fixture
def browser(): yield webdriver.Chrome()
C. fixture browser(): yield webdriver.Chrome()
D. @fixture def browser(): return webdriver.Chrome()
Solution
Step 1: Identify correct fixture decorator usage
pytest fixtures require the @pytest.fixture decorator above the function to mark it as a fixture.
Step 2: Check syntax correctness
@pytest.fixture
def browser(): yield webdriver.Chrome() correctly uses @pytest.fixture and a generator with yield to provide the resource.
Final Answer:
@pytest.fixture\ndef browser(): yield webdriver.Chrome() -> Option B
Quick Check:
Use @pytest.fixture decorator [OK]
Hint: Always prefix fixture functions with @pytest.fixture [OK]
A. driver.quit() is never called, so browser stays open after tests
B. Syntax error because return and yield cannot be used together
C. Fixture will not provide the driver to tests
D. driver.quit() runs before driver is created
Solution
Step 1: Analyze return and driver.quit() placement
Since return exits the function immediately, driver.quit() after it never runs.
Step 2: Understand impact on browser lifecycle
Without driver.quit(), the browser stays open after tests, causing resource leaks.
Final Answer:
driver.quit() is never called, so browser stays open after tests -> Option A
Quick 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
A. @pytest.fixture(scope='function')
def browser():
driver = webdriver.Chrome()
yield driver
driver.quit()
B. @pytest.fixture(scope='module')
def browser():
driver = webdriver.Chrome()
driver.quit()
yield driver
C. @pytest.fixture(scope='session')
def browser():
driver = webdriver.Chrome()
yield driver
driver.quit()
D. @pytest.fixture(scope='session')
def browser():
driver = webdriver.Chrome()
driver.quit()
yield driver
Solution
Step 1: Choose correct fixture scope for session-wide browser
Using scope='session' makes the fixture run once per test session.
Step 2: Verify setup and teardown order with yield
Driver is created before yield (browser opens), and driver.quit() runs after all tests finish.