Skip to content

Malli-K/Electrolux

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

API Automation Framework - Pytest

Overview

Automated test suite for the JSONPlaceholder Posts API: https://jsonplaceholder.typicode.com/posts

This project demonstrates a professional, maintainable test automation framework using Python and pytest.

Framework Architecture

conftest.py          → Pytest fixtures and configuration
core/                → HTTP layer (base API client)
  └── api_client.py  → Reusable API client with error handling
pages/               → Page Object Model (POM) pattern
  └── post_page.py   → Posts API abstraction layer
tests/               → Test cases
  ├── test_posts.py             → Positive tests
  └── test_negative_posts.py    → Negative/error scenario tests
data/                → Test data and constants
  └── test_data.py    → Test data definitions
utils/               → Utilities
  ├── config.py      → Configuration (base URL, timeouts)
  ├── exceptions.py  → Custom exceptions
  └── logger.py      → Logging configuration

Features

Python and Pytest - Modern test framework
Positive Tests - Validates API functionality
Negative Tests - Error handling and edge cases
Schema Validation - Ensures response structure and data types
Logging - File and console logging for debugging
Exception Handling - Custom exceptions and error management
Reusable Components - API client and page objects
Maintainable Structure - Clean architecture for scalability

Requirements

  • Python 3.7+
  • pip (Python package manager)

Setup Instructions

1. Create a Virtual Environment

# On Windows
python -m venv venv
venv\Scripts\activate

# On macOS/Linux
python3 -m venv venv
source venv/bin/activate

3. Install Dependencies

pip install -r requirements.txt

This installs:

  • pytest - Test framework
  • requests - HTTP client library

Running the Tests

Run All Tests

pytest

Run with Verbose Output

pytest -v

Run Specific Test File

pytest tests/test_posts.py -v
pytest tests/test_negative_posts.py -v

Run Specific Test

pytest tests/test_posts.py::TestPosts::test_get_all_posts -v

Run with Logging

pytest --log-cli-level=INFO

Test Coverage

Positive Tests (test_posts.py)

  1. test_get_all_posts - Validates fetching all posts

    • Checks HTTP 200 status
    • Verifies response is a list
    • Validates schema and data types of each post
  2. test_get_single_post - Validates fetching a post by ID

    • Checks HTTP 200 status
    • Verifies correct post ID in response
    • Validates schema and data types
    • Ensures non-empty required fields

Negative Tests (test_negative_posts.py)

  1. test_get_post_with_invalid_id - Tests handling of non-existent IDs

    • Expects HTTP 404 status
    • Verifies empty response body
  2. test_get_post_with_out_of_range_id - Tests out-of-range IDs

    • Expects HTTP 404 status
  3. test_get_post_with_invalid_data_type - Tests invalid input handling

    • Tests with non-integer ID values
    • Verifies proper error handling

API Validation

Validated Post Schema

Each post object is validated to contain:

  • userId (integer, positive value)
  • id (integer, positive value)
  • title (non-empty string)
  • body (non-empty string)

Configuration

Edit utils/config.py to customize:

BASE_URL = "https://jsonplaceholder.typicode.com/"  # API base URL
TIMEOUT = 10  # Request timeout in seconds

Logging

Logs are generated in the logs/ directory:

  • Console: INFO and above
  • File: DEBUG and above

Configure in utils/logger.py

Error Handling

Custom exception APIException is raised for:

  • Connection errors
  • Timeout errors
  • HTTP errors
  • Unexpected errors

Dependencies

See requirements.txt:

  • pytest>=7.0.0
  • requests>=2.25.0

Best Practices Implemented

✓ Page Object Model (POM) pattern for maintainability
✓ Centralized configuration
✓ Comprehensive logging
✓ Reusable fixtures
✓ Clear test naming
✓ Detailed assertions with messages
✓ Schema validation
✓ Error scenario testing
✓ DRY (Don't Repeat Yourself) principle

Troubleshooting

Import Errors

Ensure you're in the project root directory when running pytest.

Connection Errors

Verify the API is accessible: https://jsonplaceholder.typicode.com/posts

Timeout Errors

Increase timeout in utils/config.py:

TIMEOUT = 20  # Increase to 20 seconds

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages