0
0
Selenium Pythontesting~3 mins

Why Conftest for shared fixtures in Selenium Python? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

Discover how one file can save you hours of repetitive test setup work!

The Scenario

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.

The Problem

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.

The Solution

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.

Before vs After
Before
from selenium import webdriver

def test_login():
    driver = webdriver.Chrome()
    driver.get('url')
    # login steps
    # test steps
    driver.quit()
After
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
What It Enables

It enables writing clean, reusable test setups that save time and reduce mistakes across many tests.

Real Life Example

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.

Key Takeaways

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.