Skip to content

This project serves as a reference implementation for writing clean, maintainable, and effective API tests in Python

License

Notifications You must be signed in to change notification settings

Nikita-Filonov/python-api-tests

Repository files navigation

Python API tests

Links

Overview

This project provides an example of API testing for a REST API using Python. It incorporates powerful libraries and best practices to ensure maintainability, readability, and efficiency. Key technologies and methodologies used in this project include:

  • HTTPX – A powerful and efficient HTTP client for making API requests.
  • Pydantic – Used for data validation, deserialization, and serialization.
  • Pydantic Settings – Manages test settings in a structured and maintainable way.
  • Faker – Generates fake data for testing purposes.
  • Pytest – A full-featured and powerful testing framework in Python.
  • JSON Schema validation – Ensures contract testing by validating API responses against predefined schemas.
  • Allure – A comprehensive test reporting framework that provides detailed and visually appealing reports.
  • Assertions in separate functions – Encourages reusable and atomic test assertions.
  • API client abstraction – Encapsulates API interaction logic for better modularity and reusability.
  • Base API client – Serves as the entry point for all HTTP calls.
  • Logging features – Improves test readability and debugging, especially in CI/CD environments.
  • Routing as Enum – Replaces raw string URLs with an enumerated routing system for better maintainability.
  • Pytest plugins – Moves fixtures out of conftest.py to keep the test suite organized and manageable.
  • HTTPX event hooks – Enables logging and additional features without modifying the original API client code.

This project targets SampleAPIs, which provides various fake API datasets for testing. Specifically, this project works with the FakeBank API and uses its endpoints for testing purposes.

Setup Instructions

Prerequisites

Ensure that you have the following installed on your system:

  • Python 3.11 or later
  • pip (Python package manager)
  • Git

Installation

Clone the repository and navigate to the project directory:

git clone https://github.com/Nikita-Filonov/python-api-tests.git
cd python-api-tests

Create and activate a virtual environment:

python -m venv venv # Create virtual environment
source venv/bin/activate # Activate on macOS/Linux
venv\Scripts\activate # Activate on Windows

Install dependencies:

pip install --upgrade pip # Upgrade pip to the latest version
pip install -r requirements.txt # Install required dependencies

Running Tests

To run API tests using pytest:

pytest -m regression --numprocesses 2 # Run regression tests in parallel

Generating Allure Reports

Run tests and generate Allure results:

pytest -m regression --alluredir=allure-results

To serve the Allure report locally:

allure serve allure-results

Running Tests in CI/CD

Tests are automatically executed in a CI/CD pipeline using GitHub Actions. The workflow is configured to:

  • Run tests on every push and pull request to the main branch.
  • Generate and upload Allure reports as artifacts.
  • Publish the Allure report to GitHub Pages for easy access.

Ensure that the gh-pages branch exists in your repository for successful deployment. If it does not exist, create it manually:

git checkout --orphan gh-pages

Then push the new branch:

git push origin gh-pages

To allow GitHub Actions to publish the report, enable Workflow permissions:

  • Open your repository on GitHub.
  • Go to Settings > Actions > General.
  • Scroll down to Workflow permissions.
  • Select Read and write permissions.
  • Click Save.

Once set up, your tests will run automatically, and the Allure report will be deployed to GitHub Pages.

Accessing Allure Reports

After a successful test run in CI/CD:

  • The Allure report will be available at GitHub Pages.
  • The workflow logs and artifacts can be accessed via GitHub Actions.
  • If the pages build and deployment workflow does not appear, verify your GitHub Pages settings:
    • Go to Settings > Pages.
    • Under Build and deployment, ensure the source is set to the gh-pages branch.

Documentation for Used Actions

For detailed information on GitHub Actions used in this project, refer to the following:

Summary

This project serves as a reference implementation for writing clean, maintainable, and effective API tests in Python. It showcases best practices such as using a dedicated API client, structured assertions, logging, and CI/CD integration with Allure reporting. The setup allows for easy test execution locally and in CI/CD environments, ensuring high-quality automated testing with minimal manual intervention.