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:
- Web UI testing for SauceDemo (https://www.saucedemo.com/)
- API testing for Restful-booker (https://restful-booker.herokuapp.com/), including authentication.
- 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.
- 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.
- Clone the Repository:
git clone https://github.com/yashwant-das/pytest-web-api-testing-framework.git
cd pytest-web-api-testing-framework- 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.)
- Install Dependencies:
pip install -r requirements.txt- Set Up Environment Variables:
cp config/.env.example config/.envEdit 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 todev,staging, etc. Defaults todevif not set.
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
(
-vfor verbose,-sto show print statements and logs) -
Run Tests in Parallel (if
pytest-xdistis installed):pytest -n auto # Use all CPU cores pytest -n 4 # Run with 4 workers
- Basic HTML Report (pytest-html):
pytest --html=reports/pytest_report.html --self-contained-htmlOpen reports/pytest_report.html in your browser.
- 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
The test results are automatically published to GitHub Pages after each CI/CD run. You can view them at:
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
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
config/: Manages all external configurations, including URLs, browser settings, and credentials. Uses.envfor sensitive data.src/base/: Foundational classes.driver_factory.pyprovides WebDriver instances.web_base.pyoffers common Selenium interactions.api_base.pyprovides 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 mainconfigfixture.
Configuration is loaded in this order:
config/.env: Loaded into environment variables usingpython-dotenv.TEST_ENVEnvironment Variable: Determines which environment-specific config to use. Defaults todev.config/config.json: Base configuration.config/config_<TEST_ENV>.json: Environment-specific overrides.- Credential Injection: The
configfixture resolves credential placeholders in JSON configs using environment variables.
This layered approach enables flexible and secure management of test settings across environments.
Uses Black (formatting), isort (import sorting), and Flake8 (linting). Configurations are in pyproject.toml.
- Sort imports:
isort .- Format code:
black .- Check for style and logical errors:
flake8 .- Install pre-commit:
pip install pre-commit- Set up Git hooks:
pre-commit install- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeature). - Make your changes.
- Ensure all tests pass (
pytest). - Ensure code is formatted and linted.
- Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/YourFeature). - Open a Pull Request.
This project is licensed under the MIT License - see the LICENSE.md file for details.