Skip to content

shubhamjaiminy/python-pytest-framework

Repository files navigation

Python Pytest Framework

A comprehensive test automation framework built with Python and pytest, designed for API, UI, database, and integration testing.

Project Structure

python-pytest-framework/
├── .github/workflows/          # GitHub Actions CI/CD workflows
│   ├── smoke.yml              # Smoke test pipeline
│   ├── regression.yml         # Regression test pipeline
│   └── nightly.yml            # Nightly test suite
├── config/                     # Configuration files
│   ├── config.yaml            # Base configuration
│   ├── dev.yaml               # Development environment
│   ├── qa.yaml                # QA environment
│   ├── stage.yaml             # Staging environment
│   └── prod.yaml              # Production environment
├── core/                       # Core framework modules
│   ├── api/                   # API client utilities
│   ├── ui/                    # UI automation utilities
│   ├── db/                    # Database utilities
│   ├── auth/                  # Authentication helpers
│   ├── drivers/               # WebDriver management
│   ├── clients/               # HTTP clients
│   ├── logger/                # Logging utilities
│   └── reporting/             # Report generation
├── pages/                      # Page Object Model classes
├── api/                        # API endpoint definitions
├── database/                   # Database models and queries
├── utilities/                  # Helper utilities
├── fixtures/                   # Pytest fixtures
├── testdata/                   # Test data files
├── schemas/                    # JSON/API schemas
├── tests/                      # Test suites
│   ├── api/                   # API tests
│   ├── ui/                    # UI tests
│   ├── db/                    # Database tests
│   ├── integration/           # Integration tests
│   ├── contract/              # Contract tests
│   ├── performance/           # Performance tests
│   └── security/              # Security tests
├── reports/                    # Test reports
├── screenshots/                # Test screenshots
├── logs/                       # Test logs
├── Dockerfile                  # Docker image definition
├── docker-compose.yml          # Docker Compose setup
├── requirements.txt            # Python dependencies
├── pytest.ini                  # Pytest configuration
├── conftest.py                 # Pytest configuration hooks
└── README.md                   # Project documentation

Setup and Installation

Prerequisites

  • Python 3.11+
  • pip or conda
  • Docker and Docker Compose (optional)

Local Setup

  1. Clone the repository:
git clone <repository-url>
cd python-pytest-framework
  1. Create a virtual environment:
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate
  1. Install dependencies:
pip install -r requirements.txt
  1. Configure environment:
cp config/dev.yaml config/local.yaml
# Edit config/local.yaml as needed

Docker Setup

Build and run using Docker Compose:

docker-compose up --build

Running Tests

Run all tests:

pytest tests/ -v

Run specific test category:

pytest tests/api/ -v              # API tests
pytest tests/ui/ -v               # UI tests
pytest tests/db/ -v               # Database tests
pytest tests/integration/ -v      # Integration tests

Run tests with markers:

pytest -m smoke -v                # Smoke tests
pytest -m regression -v           # Regression tests
pytest -m api -v                  # API tests
pytest -m ui -v                   # UI tests

Run with coverage:

pytest tests/ --cov=. --cov-report=html

Run tests for specific environment:

pytest tests/ --env=qa -v
pytest tests/ --env=prod -v

Run tests in parallel:

pytest tests/ -n auto

Test Organization

  • Smoke Tests: Quick validation tests that run frequently
  • Regression Tests: Comprehensive tests ensuring no functionality breaks
  • API Tests: Tests for REST/GraphQL endpoints
  • UI Tests: Browser-based UI automation tests
  • Database Tests: Database integration and query tests
  • Integration Tests: Multi-component integration tests
  • Contract Tests: API contract validation tests
  • Performance Tests: Load and performance testing
  • Security Tests: Security vulnerability scanning

Configuration

Configuration is environment-specific:

  • config/config.yaml: Base configuration
  • config/dev.yaml: Development settings
  • config/qa.yaml: QA environment settings
  • config/stage.yaml: Staging environment settings
  • config/prod.yaml: Production environment settings

Load configuration in tests using the env_config fixture:

def test_api_call(env_config):
    base_url = env_config['api']['base_url']
    # Test code here

CI/CD Integration

The framework includes GitHub Actions workflows:

  • smoke.yml: Runs smoke tests on push and pull requests
  • regression.yml: Runs regression tests daily
  • nightly.yml: Runs complete test suite nightly

Logging and Reporting

  • Logs are saved to logs/ directory
  • Test reports are generated in reports/ directory
  • Screenshots are captured in screenshots/ directory on failures
  • HTML reports are generated using pytest-html
  • Allure reports are available using allure-pytest

Best Practices

  1. Page Object Model: Use page objects for UI tests (see pages/)
  2. Test Data: Keep test data in testdata/ directory
  3. Fixtures: Use pytest fixtures defined in fixtures/
  4. Markers: Tag tests appropriately for selective execution
  5. Logging: Use core logger for consistent logging
  6. Error Handling: Capture screenshots on UI test failures
  7. Documentation: Add docstrings to test functions

Troubleshooting

  • Module not found: Ensure virtual environment is activated
  • WebDriver issues: Update browser drivers in core/drivers/
  • Database connection: Verify database configuration in config/
  • Test failures: Check logs in logs/ and screenshots in screenshots/

Contributing

  1. Create a feature branch
  2. Write tests for new functionality
  3. Ensure all tests pass locally
  4. Submit a pull request

License

MIT License - see LICENSE file for details

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors