Skip to content

petiokasmetio/python-api-test-automation-framework

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python API Test Automation Framework

Python-based API test automation framework using pytest, requests and JSON schema validation.

This project demonstrates how to build a reusable API testing framework with an object-oriented client layer, pytest fixtures, parametrized tests, schema validation, environment-based configuration and CI execution.

Disclaimer

This project is created for QA automation practice, learning and portfolio demonstration purposes.

It uses the public JSONPlaceholder API, which provides fake REST endpoints for testing and prototyping. The project does not interact with private systems, production business data or confidential information.

Write operations such as POST, PUT, PATCH and DELETE are simulated by JSONPlaceholder and are not persisted.

Overview

The framework tests public REST API endpoints related to:

  • posts
  • users
  • todos
  • comments linked to posts

The project is structured as a small but realistic API automation framework, not just a collection of standalone tests.

Key Features

  • Python API test automation
  • pytest-based test execution
  • requests-based HTTP client
  • object-oriented API client layer
  • reusable pytest fixtures
  • parametrized tests
  • JSON schema validation
  • reusable assertion utilities
  • environment-based configuration
  • HTML report generation
  • GitHub Actions CI workflow

Technologies Used

  • Python
  • pytest
  • requests
  • jsonschema
  • pytest-html
  • python-dotenv
  • GitHub Actions

API Under Test

Base URL:

https://jsonplaceholder.typicode.com

Tested resources:

/posts
/posts/{id}
/posts/{id}/comments
/users
/users/{id}
/todos
/todos/{id}

Project Structure

python-api-test-automation-framework
├── api
│   ├── base_client.py
│   ├── posts_client.py
│   ├── todos_client.py
│   └── users_client.py
├── config
│   └── settings.py
├── schemas
│   ├── post_schema.json
│   ├── todo_schema.json
│   └── user_schema.json
├── tests
│   ├── conftest.py
│   ├── test_posts.py
│   ├── test_todos.py
│   └── test_users.py
├── utils
│   ├── assertions.py
│   └── schema_validator.py
├── .github
│   └── workflows
│       └── python-tests.yml
├── .env.example
├── .gitignore
├── pytest.ini
├── requirements.txt
└── README.md

Framework Design

API Client Layer

The framework uses an object-oriented API client layer.

BaseApiClient contains reusable HTTP methods:

GET
POST
PUT
PATCH
DELETE

Domain-specific clients extend the base client:

PostsClient
UsersClient
TodosClient

This keeps the tests clean and avoids direct requests calls inside test cases.

pytest Fixtures

Reusable pytest fixtures are defined in tests/conftest.py.

They provide:

  • posts client
  • users client
  • todos client
  • sample request payloads

Schema Validation

JSON schema files are stored under the schemas/ folder.

The framework validates API responses against schemas for:

  • posts
  • users
  • todos

Assertion Utilities

Reusable assertion helpers are stored in utils/assertions.py.

They support:

  • status code validation
  • required key validation
  • non-empty list validation

Test Coverage

The current version includes 19 passing tests.

Covered scenarios include:

GET all posts
GET post by ID
GET comments for a post
POST create post
PUT update post
PATCH update post
DELETE post
GET all users
GET user by ID
Validate user email format
GET all todos
GET todo by ID
Validate todo completed field type

How to Run Locally

Create and activate a virtual environment:

python -m venv .venv

Windows PowerShell:

.\.venv\Scripts\Activate.ps1

Install dependencies:

python -m pip install --upgrade pip
python -m pip install -r requirements.txt

Run all tests:

python -m pytest

Run tests with verbose output:

python -m pytest -v

Run smoke tests:

python -m pytest -m smoke

Generate an HTML report:

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

Example Test Result

Current local execution result:

19 passed

Configuration

Default configuration is stored in config/settings.py.

The framework also supports environment variables through .env.

Example .env values:

BASE_URL=https://jsonplaceholder.typicode.com
API_TIMEOUT=10

CI/CD

The project includes a GitHub Actions workflow that installs dependencies and runs the API test suite on push and pull request events.

Workflow file:

.github/workflows/python-tests.yml

QA Skills Demonstrated

This project demonstrates:

  • Python test automation
  • API testing
  • pytest fixtures
  • pytest parametrization
  • requests-based HTTP testing
  • object-oriented test framework design
  • JSON schema validation
  • reusable assertions
  • environment configuration
  • HTML test reporting
  • GitHub Actions CI
  • practical SDET mindset

Limitations

Current limitations:

  • public API availability may affect test execution
  • write operations are simulated and not persisted
  • no advanced logging yet
  • no Allure reporting yet
  • no negative contract testing yet
  • no authentication scenarios yet

Potential Future Improvements

Possible next improvements:

  • add negative API scenarios
  • add authentication examples using a different demo API
  • add response time assertions
  • add Allure reporting
  • add request/response logging
  • add data-driven tests from JSON files
  • add Pydantic models for response validation
  • add more schema validation coverage
  • add Docker support

Author

Petar Nikolov

Test Automation Engineering Analyst with experience in ERP and banking systems, Java/Python automation, Selenium, API testing, Cucumber BDD, QA tooling, XML test data generation and business-critical software testing.

About

Python pytest API automation framework with reusable clients, fixtures, schema validation and CI.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages