0
0
PyTesttesting~8 mins

Test classes in PyTest - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Test classes
Folder Structure
tests/
├── test_login.py
├── test_shopping_cart.py
├── test_checkout.py
└── conftest.py

src/
├── pages/
│   ├── login_page.py
│   ├── cart_page.py
│   └── checkout_page.py
└── utils/
    ├── browser.py
    └── helpers.py
Test Framework Layers
  • Test Classes: Group related test functions inside classes for better organization and shared setup/teardown.
  • Page Objects: Python classes representing UI pages with methods to interact with page elements.
  • Utilities: Helper functions and browser setup code to support tests.
  • Configuration: Settings for environment, browser, and credentials managed in conftest.py and config files.
Configuration Patterns
  • conftest.py: Define fixtures here for browser setup, test data, and environment variables.
  • pytest.ini or tox.ini: Store pytest options and markers.
  • Environment Variables: Use OS environment variables or .env files to manage sensitive data like credentials.
  • Command Line Options: Use pytest hooks to accept parameters like --browser or --env for flexible test runs.
Test Reporting and CI/CD Integration
  • Use pytest built-in reports with --junitxml=report.xml for XML reports consumable by CI tools.
  • Integrate with CI/CD pipelines (GitHub Actions, Jenkins, GitLab CI) to run tests automatically on code changes.
  • Use plugins like pytest-html for readable HTML reports.
  • Configure test failure screenshots or logs for easier debugging.
Best Practices for Test Classes in pytest
  • Group related tests in classes without __init__ methods to share fixtures via self or class-level fixtures.
  • Use setup_method and teardown_method or pytest fixtures for setup and cleanup.
  • Keep test methods small and focused on one behavior.
  • Name test classes starting with Test and test methods starting with test_ for pytest discovery.
  • Avoid state sharing between tests to keep tests independent and reliable.
Self Check

Where would you add a new test class for user profile features in this framework structure?

Key Result
Organize related test functions inside pytest test classes for clear structure and shared setup.