0
0
Selenium Pythontesting~8 mins

Conftest for shared fixtures in Selenium Python - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Conftest for shared fixtures
Folder Structure
selenium-python-project/
├── tests/
│   ├── test_login.py
│   ├── test_checkout.py
│   └── test_profile.py
├── pages/
│   ├── login_page.py
│   ├── checkout_page.py
│   └── profile_page.py
├── utils/
│   └── helpers.py
├── conftest.py
└── pytest.ini
  
Test Framework Layers
  • Driver Layer: Managed by fixtures in conftest.py to create and close Selenium WebDriver instances.
  • Page Objects: Classes in pages/ folder representing UI pages with methods for user actions.
  • Test Cases: Test scripts in tests/ folder using pytest and shared fixtures.
  • Utilities: Helper functions in utils/ for reusable code like waits or data generation.
  • Configuration: pytest.ini for pytest settings and conftest.py for shared fixtures and hooks.
Configuration Patterns

Use conftest.py to define shared fixtures that provide WebDriver setup and teardown. Example patterns:

  • Browser choice: Use pytest command line options to select browser (e.g., Chrome, Firefox).
  • Environment variables: Store URLs and credentials in environment variables or config files, accessed in fixtures.
  • Fixture scope: Use scope="function" for fresh browser per test or scope="session" for reuse.

Example snippet in conftest.py:

import pytest
from selenium import webdriver

@pytest.fixture(scope="function")
def driver(request):
    browser = request.config.getoption("browser") or "chrome"
    if browser == "chrome":
        driver = webdriver.Chrome()
    elif browser == "firefox":
        driver = webdriver.Firefox()
    else:
        raise ValueError(f"Unsupported browser: {browser}")
    driver.maximize_window()
    yield driver
    driver.quit()

# Add pytest_addoption to accept --browser CLI argument

def pytest_addoption(parser):
    parser.addoption(
        "--browser",
        action="store",
        default="chrome",
        help="Browser to run tests on: chrome or firefox"
    )
  
Test Reporting and CI/CD Integration
  • Use pytest built-in reporting with pytest --junitxml=report.xml to generate XML reports.
  • Integrate with CI tools like Jenkins, GitHub Actions, or GitLab CI to run tests on push or pull requests.
  • Use plugins like pytest-html for readable HTML reports.
  • Configure CI to install dependencies, run tests with selected browser, and archive reports.
Best Practices
  • Centralize fixtures: Put shared fixtures in conftest.py to avoid duplication and ease maintenance.
  • Use fixture scopes wisely: Choose function scope for isolation or session for speed depending on test needs.
  • Parameterize fixtures: Allow browser or environment selection via command line options for flexibility.
  • Clean up resources: Always quit WebDriver in fixture teardown to avoid leftover browser processes.
  • Keep tests independent: Use fresh fixtures or reset state to avoid flaky tests.
Self Check

Where in this framework structure would you add a new fixture to provide a logged-in user session for tests?

Key Result
Use conftest.py to define shared, reusable fixtures for WebDriver setup and test environment configuration.