0
0
PyTesttesting~8 mins

Why advanced patterns handle real-world complexity in PyTest - Framework Benefits

Choose your learning style9 modes available
Framework Mode - Why advanced patterns handle real-world complexity
Folder Structure
project-root/
├── tests/
│   ├── test_login.py
│   ├── test_checkout.py
│   ├── test_profile.py
│   └── __init__.py
├── pages/
│   ├── login_page.py
│   ├── checkout_page.py
│   └── profile_page.py
├── utils/
│   ├── browser.py
│   ├── data_loader.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── conftest.py
└── pytest.ini
Test Framework Layers
  • Driver Layer: Manages browser setup and teardown using fixtures in conftest.py.
  • Page Objects: Encapsulate UI elements and actions in pages/ folder for reusability and maintainability.
  • Tests: Actual test cases in tests/ folder using pytest functions and assertions.
  • Utilities: Helper functions and data loaders in utils/ to support tests and page objects.
  • Configuration: Environment settings and secrets stored in config/ files, loaded dynamically.
Configuration Patterns

Use config.yaml to define environments like dev, staging, and production with URLs and settings. Store sensitive data like usernames and passwords in credentials.yaml. Load these files in conftest.py using fixtures to provide tests with environment-specific data. Use command-line options to select environment and browser type dynamically.

# Example snippet from conftest.py
import pytest
import yaml

def pytest_addoption(parser):
    parser.addoption('--env', action='store', default='dev', help='Environment to run tests against')

@pytest.fixture(scope='session')
def config(request):
    env = request.config.getoption('env')
    with open('config/config.yaml') as f:
        all_configs = yaml.safe_load(f)
    return all_configs[env]
Test Reporting and CI/CD Integration

Use pytest plugins like pytest-html or pytest-allure to generate clear, visual test reports showing passed, failed, and skipped tests. Integrate tests into CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests automatically on code changes. Reports help teams quickly see test results and fix issues early.

# Example GitHub Actions snippet
name: Run Tests
on: [push, pull_request]
jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.12'
      - name: Install dependencies
        run: pip install -r requirements.txt
      - name: Run tests
        run: pytest --html=report.html
      - name: Upload report
        uses: actions/upload-artifact@v3
        with:
          name: test-report
          path: report.html
Framework Design Principles
  • Separation of Concerns: Keep page logic, test logic, and utilities separate for clarity and easier maintenance.
  • Reusability: Use page objects and utility functions to avoid repeating code and reduce errors.
  • Data-Driven Testing: Use external config and data files to run tests with different inputs without changing code.
  • Explicit Fixtures: Use pytest fixtures to manage setup and teardown cleanly and share resources.
  • Clear Reporting: Generate readable reports to quickly understand test outcomes and debug failures.
Self Check

Where in this framework structure would you add a new page object for a "Search" feature?

Answer: Add a new file named search_page.py inside the pages/ folder.

Key Result
Use layered design with page objects, fixtures, and config files to handle real-world test complexity cleanly.