Test Overview
This test uses a shared fixture from conftest.py to open a browser, navigate to a page, and verify the page title. It checks that the fixture correctly provides the WebDriver instance.
This test uses a shared fixture from conftest.py to open a browser, navigate to a page, and verify the page title. It checks that the fixture correctly provides the WebDriver instance.
import pytest from selenium.webdriver.common.by import By def test_open_google(browser): browser.get('https://www.google.com') title = browser.title assert 'Google' in title # conftest.py import pytest from selenium import webdriver @pytest.fixture def browser(): driver = webdriver.Chrome() yield driver driver.quit()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | PyTest starts and loads conftest.py to find fixtures | PyTest environment ready with browser fixture available | — | PASS |
| 2 | Test function test_open_google is called with browser fixture injected | Browser fixture creates Chrome WebDriver instance | — | PASS |
| 3 | Browser navigates to 'https://www.google.com' | Chrome browser opens Google homepage | — | PASS |
| 4 | Test retrieves page title from browser | Page title is 'Google' | Check if 'Google' is in page title | PASS |
| 5 | Test asserts that 'Google' is in the page title | Assertion passes because title contains 'Google' | assert 'Google' in title | PASS |
| 6 | Test completes, browser fixture quits the WebDriver | Chrome browser closes | — | PASS |
conftest.py file in Selenium Python testing?conftest.pyconftest.py file is used to define fixtures that can be shared across multiple test files for setup and cleanup tasks.conftest.py using pytest?@pytest.fixture decorator above the function to mark it as a fixture.@pytest.fixture and a generator with yield to provide the resource.conftest.py:
@pytest.fixture
def browser():
driver = webdriver.Chrome()
yield driver
driver.quit()
What happens when a test uses this fixture?yield statement pauses the fixture, providing the driver to the test, then resumes after the test to run cleanup.driver.quit() runs after test finishes (browser closes).conftest.py:
@pytest.fixture
def browser():
driver = webdriver.Chrome()
return driver
driver.quit()
What is the problem with this fixture?return exits the function immediately, driver.quit() after it never runs.driver.quit(), the browser stays open after tests, causing resource leaks.conftest.py that opens Chrome once per test session and closes it after all tests finish. Which fixture scope and code is correct?scope='session' makes the fixture run once per test session.yield (browser opens), and driver.quit() runs after all tests finish.Pass the test and this pattern moves unknown