0
0
Testing Fundamentalstesting~8 mins

REST API testing basics in Testing Fundamentals - Framework Patterns

Choose your learning style9 modes available
Framework Mode - REST API testing basics
Folder Structure
api-test-project/
├── tests/
│   ├── test_users.py
│   ├── test_orders.py
│   └── test_products.py
├── utils/
│   ├── api_client.py
│   └── helpers.py
├── config/
│   ├── config.yaml
│   └── credentials.yaml
├── reports/
│   └── latest_report.html
├── conftest.py
└── requirements.txt
  
Test Framework Layers
  • API Client Layer: Handles sending HTTP requests (GET, POST, PUT, DELETE) and receiving responses. Encapsulates base URL, headers, and authentication.
  • Test Layer: Contains test cases that call API client methods and assert expected results like status codes and response data.
  • Utilities Layer: Helper functions for common tasks like data formatting, token generation, or response parsing.
  • Configuration Layer: Stores environment settings, API endpoints, credentials, and other variables.
Configuration Patterns

Use separate YAML or JSON files to store environment-specific data:

  • config.yaml holds base URLs for dev, staging, and production.
  • credentials.yaml stores API keys or tokens securely (avoid hardcoding in code).

Load configuration at test start to switch environments easily.

Example snippet to load config in Python:

import yaml

with open('config/config.yaml') as f:
    config = yaml.safe_load(f)

base_url = config['environments']['staging']['base_url']
Test Reporting and CI/CD Integration
  • Use test runners like pytest with plugins such as pytest-html to generate readable HTML reports.
  • Reports show passed/failed tests, error messages, and response details for failures.
  • Integrate tests into CI/CD pipelines (GitHub Actions, Jenkins) to run API tests automatically on code changes.
  • Fail the build if critical API tests fail to prevent broken APIs from deploying.
Best Practices
  1. Use clear and descriptive test names that explain what API behavior is tested.
  2. Isolate tests so each test can run independently without relying on others.
  3. Validate both status codes and response content to ensure API correctness.
  4. Use data-driven testing to test multiple input scenarios efficiently.
  5. Handle authentication securely and refresh tokens if needed during tests.
Self Check

Where would you add a new test file for testing the "payment" API endpoints in this framework structure?

Key Result
Organize REST API tests with clear layers: API client, tests, utilities, and config for maintainability and scalability.