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
Why advanced patterns handle real-world complexity in PyTest - Framework Benefits
Start learning this pattern below
Jump into concepts and practice - no test required
- 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.
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]
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
- 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.
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.
Practice
Solution
Step 1: Understand the role of fixtures
Fixtures in pytest are designed to prepare the environment before a test runs and clean up after it finishes.Step 2: Identify the benefit in complex tests
By managing setup and cleanup automatically, fixtures reduce repeated code and make tests clearer and easier to maintain.Final Answer:
They automatically handle setup and cleanup for tests. -> Option AQuick Check:
Fixtures = setup and cleanup automation [OK]
- Thinking fixtures speed up tests by skipping assertions
- Believing fixtures replace test functions
- Assuming fixtures generate random data automatically
Solution
Step 1: Recall pytest parametrize decorator syntax
The correct decorator is @pytest.mark.parametrize with the parameters as a string and a list of tuples.Step 2: Check each option
@pytest.mark.parametrize('input,expected', [(1,2), (3,4)]) uses the correct decorator with a list of tuples. Incorrect options omit '.mark.', use a flat list instead of tuples, or use a dictionary instead of a list of tuples.Final Answer:
@pytest.mark.parametrize('input,expected', [(1,2), (3,4)]) -> Option BQuick Check:
Correct decorator = @pytest.mark.parametrize [OK]
- Using @pytest.parametrize instead of @pytest.mark.parametrize
- Using a flat list like [1,2,3,4] instead of list of tuples
- Passing a dictionary instead of a list of tuples
import pytest
@pytest.mark.parametrize('x,y', [(1,2), (3,4)])
def test_sum(x, y):
assert x + y == 3Solution
Step 1: Analyze the parametrized inputs and assertion
The test runs twice: first with x=1, y=2; second with x=3, y=4. The assertion checks if x + y == 3.Step 2: Evaluate each test case
For (1,2), 1+2=3, assertion passes. For (3,4), 3+4=7, assertion fails.Final Answer:
First test passes, second test fails -> Option DQuick Check:
1+2=3 pass, 3+4=7 fail [OK]
- Assuming both tests pass without checking values
- Confusing syntax error with correct decorator usage
- Ignoring that second input fails assertion
import pytest
@pytest.fixture
def setup_data():
data = {'key': 'value'}
return data
def test_data(setup_data):
assert setup_data['key'] == 'value'Solution
Step 1: Review fixture definition and usage
The fixture 'setup_data' returns a dictionary. The test function accepts it as a parameter and asserts a key's value.Step 2: Check for common fixture errors
The fixture is correctly defined with @pytest.fixture, used as a parameter, and returns data properly. No yield is needed unless cleanup is required.Final Answer:
No error; code runs correctly -> Option CQuick Check:
Fixture usage correct = no error [OK]
- Thinking yield is mandatory in fixtures
- Forgetting to pass fixture as test parameter
- Assuming fixture name conflicts with test function
Solution
Step 1: Understand the problem of many input combinations
Writing many test functions for each input is repetitive and hard to maintain.Step 2: Identify the pytest feature for efficient input testing
@pytest.mark.parametrize allows running the same test function multiple times with different inputs automatically.Step 3: Compare options
Parametrizing tests with @pytest.mark.parametrize uses parametrization, which is the recommended advanced pattern. Using multiple assert statements, print statements to check manually, or writing separate test functions are inefficient or manual approaches.Final Answer:
Parametrizing tests with @pytest.mark.parametrize -> Option AQuick Check:
Parametrize = efficient multiple inputs [OK]
- Writing many separate test functions instead of parametrizing
- Using print instead of assertions
- Trying to test many inputs in one test without parametrization
