This project demonstrates API testing using Pytest in Python. The tests are written to validate the endpoints of the JSONPlaceholder API.
The project follows a structured design pattern to ensure maintainability and reusability. The key components include:
- api/: Contains the API client code to interact with the JSONPlaceholder API.
- config/: Configuration files for environment settings and logging.
- services/: Service classes encapsulating the business logic and interactions with the API endpoints.
- tests/: Test cases organized by API endpoints.
- utils/: Utility functions and helper methods.
- data/: JSON files for test data.
- .github/: GitHub Actions workflows for CI/CD.
project_root/
│
├── src/
│ ├── __init__.py
│ ├── api/
│ │ ├── __init__.py
│ │ ├── clients.py
│ ├── config/
│ │ ├── __init__.py
│ │ ├── logging_config.py
│ │ ├── settings.py
│ │ ├── conftest.py
│ ├── services/
│ │ ├── __init__.py
│ │ ├── post_service.py
│ │ ├── comment_service.py
│ │ ├── user_service.py
│ │ ├── album_service.py
│ │ ├── photo_service.py
│ │ ├── todo_service.py
│ ├── tests/
│ │ ├── __init__.py
│ │ ├── test_posts.py
│ │ ├── test_comments.py
│ │ ├── test_users.py
│ │ ├── test_albums.py
│ │ ├── test_photos.py
│ │ ├── test_todos.py
│ ├── utils/
│ │ ├── __init__.py
│ │ ├── helpers.py
│ │ ├── logging_utils.py
│ ├── data/
│ │ ├── albums_test_data.json
│ │ ├── comments_test_data.json
│ │ ├── photos_test_data.json
│ │ ├── posts_test_data.json
│ │ ├── todos_test_data.json
│ │ ├── users_test_data.json
│
├── .github/
│ ├── workflows/
│ │ ├── ci.yml
│
├── .env
├── .gitignore
├── README.md
├── requirements.txt
└── pytest.ini
Each service class in the services/ directory encapsulates the business logic and interactions with the API endpoints, promoting modularity and reusability.
Common helper methods are placed in the utils/ directory to keep the codebase DRY (Don't Repeat Yourself).
git clone <repository-url>
cd project_root
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
pip install -r requirements.txt
Create a .env file in the root directory and add any necessary environment variables.
Logging configuration is centralized in src/config/logging_config.py. The configure_logging function sets up logging with a unique log file for each test run.
The logging utility in src/utils/logging_utils.py provides a setup_logging function to configure logging for test runs based on the test name.
Test data is stored in JSON files under the src/data directory. Each test file loads the necessary test data using pytest fixtures.
Each test file initializes the necessary service and loads test data through fixtures. Logging is set up using the setup_logging function from the logging utility.
If you haven't already set up the folder structure, you can run the provided script:
python setup_structure.py
pytest
This project uses GitHub Actions for Continuous Integration. The configuration file is located at .github/workflows/ci.yml.
You can replace <repository-url> with the actual URL of your repository.