Skip to content

Comprehensive API testing framework with support for REST, GraphQL, and WebSocket testing. Built with Python for automated API validation and performance testing.

License

Notifications You must be signed in to change notification settings

maxxunit1/api-testing-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

API Testing Framework

Comprehensive API testing framework with support for REST, GraphQL, and WebSocket testing. Built with Python for automated API validation and performance testing.

✨ Features

  • 🌐 REST API Testing - Complete HTTP methods support (GET, POST, PUT, DELETE, PATCH)
  • πŸ” Authentication - Support for Bearer tokens, OAuth2, API keys, Basic Auth
  • βœ… Response Validation - JSON Schema validation, status code checking, response time monitoring
  • πŸ“Š Performance Testing - Load testing capabilities with concurrent requests
  • πŸ“ Detailed Reporting - HTML and JSON test reports with detailed metrics
  • πŸ”„ CI/CD Integration - Easy integration with Jenkins, GitHub Actions, GitLab CI
  • 🎯 Data-Driven Testing - Parameterized tests with CSV/JSON data sources
  • πŸ› οΈ Custom Assertions - Extensible assertion library for complex validations

πŸš€ Quick Start

Installation

# Clone the repository
git clone https://github.com/YOUR_USERNAME/api-testing-framework.git
cd api-testing-framework

# Create virtual environment
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Basic Usage

# Run all tests
pytest

# Run specific test file
pytest tests/test_api_endpoints.py

# Run with verbose output
pytest -v

# Run with coverage report
pytest --cov=framework --cov-report=html

# Run tests in parallel
pytest -n 4

πŸ“‹ Configuration

Environment Setup

Create a .env file in the root directory:

API_BASE_URL=https://api.example.com
API_KEY=your-api-key-here
AUTH_TOKEN=your-auth-token
TIMEOUT=30

Endpoints Configuration

Edit config/endpoints.json to define your API endpoints:

{
  "users": {
    "list": "/api/v1/users",
    "create": "/api/v1/users",
    "get": "/api/v1/users/{id}",
    "update": "/api/v1/users/{id}",
    "delete": "/api/v1/users/{id}"
  },
  "posts": {
    "list": "/api/v1/posts",
    "create": "/api/v1/posts"
  }
}

πŸ§ͺ Writing Tests

Simple GET Request Test

from framework.api_client import APIClient
from framework.validators import validate_response

def test_get_users():
    client = APIClient()
    response = client.get('/api/v1/users')
    
    # Validate response
    assert response.status_code == 200
    validate_response(response, 'users_schema.json')
    
    # Check response data
    data = response.json()
    assert len(data) > 0
    assert 'id' in data[0]
    assert 'email' in data[0]

POST Request with Authentication

def test_create_user():
    client = APIClient()
    
    payload = {
        "name": "John Doe",
        "email": "john@example.com",
        "role": "user"
    }
    
    response = client.post(
        '/api/v1/users',
        json=payload,
        headers={'Authorization': 'Bearer token123'}
    )
    
    assert response.status_code == 201
    data = response.json()
    assert data['email'] == payload['email']

Data-Driven Testing

import pytest

@pytest.mark.parametrize("user_id,expected_status", [
    (1, 200),
    (999, 404),
    (0, 400)
])
def test_get_user_by_id(user_id, expected_status):
    client = APIClient()
    response = client.get(f'/api/v1/users/{user_id}')
    assert response.status_code == expected_status

πŸ“Š Test Reporting

Generate HTML Report

pytest --html=reports/report.html --self-contained-html

Generate JSON Report

pytest --json-report --json-report-file=reports/report.json

View Coverage Report

pytest --cov=framework --cov-report=html
open htmlcov/index.html

πŸ”§ Framework Components

API Client (framework/api_client.py)

The core HTTP client with built-in retry logic, timeout handling, and logging:

from framework.api_client import APIClient

client = APIClient(
    base_url='https://api.example.com',
    timeout=30,
    retry_count=3
)

# Make requests
response = client.get('/endpoint')
response = client.post('/endpoint', json=data)
response = client.put('/endpoint/{id}', json=data)
response = client.delete('/endpoint/{id}')

Validators (framework/validators.py)

Response validation utilities:

from framework.validators import (
    validate_response,
    validate_status_code,
    validate_response_time,
    validate_json_schema
)

# Validate response
validate_response(response, schema_file='user_schema.json')

# Validate specific aspects
validate_status_code(response, 200)
validate_response_time(response, max_time=2.0)
validate_json_schema(response.json(), schema)

Helpers (framework/helpers.py)

Utility functions for common tasks:

from framework.helpers import (
    generate_random_email,
    generate_test_data,
    wait_for_condition,
    retry_on_failure
)

# Generate test data
email = generate_random_email()
data = generate_test_data('user')

# Wait for condition
wait_for_condition(lambda: check_status(), timeout=30)

🎯 Advanced Features

Performance Testing

from framework.api_client import APIClient
from concurrent.futures import ThreadPoolExecutor
import time

def test_load_testing():
    client = APIClient()
    
    def make_request():
        return client.get('/api/v1/users')
    
    start_time = time.time()
    
    # Send 100 concurrent requests
    with ThreadPoolExecutor(max_workers=10) as executor:
        futures = [executor.submit(make_request) for _ in range(100)]
        responses = [f.result() for f in futures]
    
    duration = time.time() - start_time
    
    # Calculate metrics
    success_rate = sum(1 for r in responses if r.status_code == 200) / len(responses)
    avg_response_time = sum(r.elapsed.total_seconds() for r in responses) / len(responses)
    
    print(f"Duration: {duration:.2f}s")
    print(f"Success Rate: {success_rate * 100:.2f}%")
    print(f"Avg Response Time: {avg_response_time:.3f}s")
    
    assert success_rate >= 0.95  # 95% success rate
    assert avg_response_time < 1.0  # Under 1 second

Custom Assertions

from framework.validators import CustomValidator

class UserValidator(CustomValidator):
    def validate_user_data(self, user):
        assert 'id' in user, "User must have ID"
        assert 'email' in user, "User must have email"
        assert '@' in user['email'], "Invalid email format"
        assert user['id'] > 0, "User ID must be positive"
        return True

validator = UserValidator()
validator.validate_user_data(response.json())

πŸ”„ CI/CD Integration

GitHub Actions

name: API Tests

on: [push, pull_request]

jobs:
  test:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v2
      
      - name: Set up Python
        uses: actions/setup-python@v2
        with:
          python-version: 3.9
      
      - name: Install dependencies
        run: |
          pip install -r requirements.txt
      
      - name: Run tests
        run: |
          pytest --html=reports/report.html
        env:
          API_BASE_URL: ${{ secrets.API_BASE_URL }}
          API_KEY: ${{ secrets.API_KEY }}
      
      - name: Upload test results
        uses: actions/upload-artifact@v2
        with:
          name: test-results
          path: reports/

Jenkins Pipeline

pipeline {
    agent any
    
    stages {
        stage('Install') {
            steps {
                sh 'pip install -r requirements.txt'
            }
        }
        
        stage('Test') {
            steps {
                sh 'pytest --junitxml=reports/junit.xml'
            }
        }
        
        stage('Report') {
            steps {
                junit 'reports/junit.xml'
            }
        }
    }
}

πŸ“š Project Structure

api-testing-framework/
β”œβ”€β”€ tests/                      # Test files
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ test_api_endpoints.py  # Endpoint tests
β”‚   β”œβ”€β”€ test_authentication.py # Auth tests
β”‚   └── test_data_validation.py # Data validation tests
β”œβ”€β”€ framework/                  # Core framework code
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ api_client.py          # HTTP client
β”‚   β”œβ”€β”€ validators.py          # Response validators
β”‚   └── helpers.py             # Utility functions
β”œβ”€β”€ config/                     # Configuration files
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ config.py              # Settings
β”‚   └── endpoints.json         # API endpoints
β”œβ”€β”€ reports/                    # Test reports
β”œβ”€β”€ schemas/                    # JSON schemas
β”œβ”€β”€ README.md                   # Documentation
β”œβ”€β”€ requirements.txt            # Dependencies
β”œβ”€β”€ pytest.ini                  # Pytest configuration
└── .gitignore                 # Git ignore rules

πŸ› οΈ Troubleshooting

Common Issues

Issue: SSL Certificate Verification Failed

client = APIClient(verify_ssl=False)

Issue: Timeout Errors

client = APIClient(timeout=60)  # Increase timeout

Issue: Connection Refused

  • Check if API server is running
  • Verify correct base URL
  • Check firewall settings

🀝 Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“„ License

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

πŸ™ Acknowledgments

πŸ“ž Support

For issues and questions:

  • Create an issue on GitHub
  • Check documentation
  • Review existing test examples

Built with ❀️ for API testing automation

Update 2025-10-13 17:36:42

Improved: 2025-10-13 17:36:42

Additional configuration

Update 2025-10-22 23:30:54

async def async_operation(): """Async operation support""" result = await fetch_data() return process(result)

About

Comprehensive API testing framework with support for REST, GraphQL, and WebSocket testing. Built with Python for automated API validation and performance testing.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages