Skip to content

Python framework for Web (Selenium) & API (Requests) testing using Pytest. Includes demos for SauceDemo & Restful-booker.

License

Notifications You must be signed in to change notification settings

yashwant-das/pytest-web-api-testing-framework

Repository files navigation

pytest-web-api-testing-framework

Python CI Tests Python License

A robust and scalable Python framework for Web (Selenium) and API (Requests) testing, built with Pytest. Designed for clarity, maintainability, and ease of extension.

Showcasing:

Core Principles

  • Separation of Concerns: Distinct responsibilities for different framework components.
  • Readability & Maintainability: Clean, understandable, and easy-to-modify code.
  • Scalability: Easily accommodate new tests, pages, and API endpoints.
  • Reusability: Shared common functionalities and base classes.
  • Configuration Driven: Externalized settings for URLs, credentials, and browser types.

Prerequisites

  • Python 3.9+ (python3 --version)
  • pip (Python package installer)
  • Git (for cloning the repository)
  • (Optional, for Allure reports) Allure Commandline installed and in your system's PATH.

Setup Instructions

  1. Clone the Repository:
git clone https://github.com/yashwant-das/pytest-web-api-testing-framework.git
cd pytest-web-api-testing-framework
  1. Create and Activate a Virtual Environment (Recommended):
  • On macOS/Linux:

    python3 -m venv venv
    source venv/bin/activate
  • On Windows:

    python -m venv venv
    .\venv\Scripts\activate

    (You should see (venv) at the beginning of your command prompt.)

  1. Install Dependencies:
pip install -r requirements.txt
  1. Set Up Environment Variables:
cp config/.env.example config/.env

Edit config/.env and replace placeholders with actual credentials:

  • SAUCE_USERNAME: standard_user (for SauceDemo)
  • SAUCE_PASSWORD: secret_sauce (for SauceDemo)
  • BOOKER_USERNAME: admin (for Restful-booker API)
  • BOOKER_PASSWORD: password123 (for Restful-booker API)
  • TEST_ENV: (Optional) Set to dev, staging, etc. Defaults to dev if not set.

Running Tests

This framework uses Pytest as its test runner.

  • Run All Tests:

    pytest
  • Run Specific Test Types (using markers):

    • Web UI tests:
    pytest -m web
    • API tests:
    pytest -m api
    • Smoke tests:
    pytest -m smoke
    • Regression tests:
    pytest -m regression
  • Run Tests in a Specific File or Directory:

    pytest tests/web/test_login_scenarios.py
    pytest tests/api/
  • Run Tests with More Verbosity and Output:

    pytest -v -s

    (-v for verbose, -s to show print statements and logs)

  • Run Tests in Parallel (if pytest-xdist is installed):

    pytest -n auto  # Use all CPU cores
    pytest -n 4     # Run with 4 workers

Generating Test Reports

  1. Basic HTML Report (pytest-html):
pytest --html=reports/pytest_report.html --self-contained-html

Open reports/pytest_report.html in your browser.

  1. Allure Report (Recommended):
  • Step 1: Run tests and generate Allure results:

    pytest --alluredir=reports/allure-results
  • Step 2: Generate the HTML report:

    allure generate reports/allure-results -o reports/allure-report --clean
  • Step 3: Open the Allure report:

    allure open reports/allure-report

CI/CD Test Results

The test results are automatically published to GitHub Pages after each CI/CD run. You can view them at:

Live Test Reports

The automated CI/CD pipeline:

  • Runs tests on multiple Python versions (3.9, 3.10, 3.11)
  • Generates comprehensive Allure reports
  • Automatically deploys reports to GitHub Pages
  • Maintains test history for trend analysis

Framework Architecture & Directory Structure

The framework is organized for separation of concerns and maintainability.

pytest-web-api-testing-framework/
├── .github/
│   └── workflows/
│       └── ci.yml
├── .gitignore
├── README.md
├── requirements.txt
├── pytest.ini
├── pyproject.toml
├── .pre-commit-config.yaml
├── conftest.py
│
├── config/
│   ├── config.json
│   ├── config_dev.json
│   ├── config_staging.json
│   ├── .env.example
│   └── .env
│
├── src/
│   ├── base/
│   │   ├── api_base.py
│   │   ├── web_base.py
│   │   └── driver_factory.py
│   │
│   ├── pages/
│   │   ├── login_page.py
│   │   └── home_page.py
│   │
│   ├── api_clients/
│   │   └── booking_service.py
│   │
│   └── utils/
│       ├── logger.py
│       ├── data_generator.py
│       └── assertions.py
│
├── tests/
│   ├── conftest.py
│   ├── web/
│   │   └── test_login_scenarios.py
│   │
│   └── api/
│       └── test_booking_api.py
│
└── reports/
   ├── allure-results/
   ├── allure-report/
   └── pytest_report.html

Key Components

  • config/: Manages all external configurations, including URLs, browser settings, and credentials. Uses .env for sensitive data.
  • src/base/: Foundational classes. driver_factory.py provides WebDriver instances. web_base.py offers common Selenium interactions. api_base.py provides common API methods and authentication.
  • src/pages/: Page Object Model (POM) for web UI interactions.
  • src/api_clients/: Classes for interacting with specific API services/endpoints.
  • src/utils/: Shared utilities like logging and test data generation.
  • tests/: Test scripts, organized by type (web, api). Uses Pytest fixtures for setup/teardown.
  • conftest.py: Shared Pytest fixtures, e.g., WebDriver and API client instances, and the main config fixture.

Configuration System

Configuration is loaded in this order:

  1. config/.env: Loaded into environment variables using python-dotenv.
  2. TEST_ENV Environment Variable: Determines which environment-specific config to use. Defaults to dev.
  3. config/config.json: Base configuration.
  4. config/config_<TEST_ENV>.json: Environment-specific overrides.
  5. Credential Injection: The config fixture resolves credential placeholders in JSON configs using environment variables.

This layered approach enables flexible and secure management of test settings across environments.

Code Quality: Linting and Formatting

Uses Black (formatting), isort (import sorting), and Flake8 (linting). Configurations are in pyproject.toml.

Manual Usage

  1. Sort imports:
isort .
  1. Format code:
black .
  1. Check for style and logical errors:
flake8 .

Using Pre-commit Hooks (Recommended)

  1. Install pre-commit:
pip install pre-commit
  1. Set up Git hooks:
pre-commit install

Contributing

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/YourFeature).
  3. Make your changes.
  4. Ensure all tests pass (pytest).
  5. Ensure code is formatted and linted.
  6. Commit your changes (git commit -m 'Add some feature').
  7. Push to the branch (git push origin feature/YourFeature).
  8. Open a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

About

Python framework for Web (Selenium) & API (Requests) testing using Pytest. Includes demos for SauceDemo & Restful-booker.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages