Skip to content

Commit 76a9525

Browse files
author
Nikita Filonov
committed
docs
1 parent c50859c commit 76a9525

File tree

2 files changed

+156
-8
lines changed

2 files changed

+156
-8
lines changed

README.md

Lines changed: 156 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,156 @@
1-
# python-api-tests
1+
# Python API tests
2+
3+
## Links
4+
5+
- [Allure Report on GitHub Pages](https://nikita-filonov.github.io/python-api-tests/8/index.html)
6+
- [GitHub Actions CI/CD](https://github.com/Nikita-Filonov/python-api-tests/actions)
7+
8+
## Overview
9+
10+
This project provides an example of API testing for a [REST API](https://en.wikipedia.org/wiki/REST) using Python. It
11+
incorporates powerful libraries and best practices to ensure maintainability, readability, and efficiency. Key
12+
technologies and methodologies used in this project include:
13+
14+
- [HTTPX](https://www.python-httpx.org/) – A powerful and efficient HTTP client for making API requests.
15+
- [Pydantic](https://docs.pydantic.dev/latest/) – Used for data validation, deserialization, and serialization.
16+
- [Pydantic Settings](https://docs.pydantic.dev/latest/concepts/pydantic_settings/) – Manages test settings in a
17+
structured and maintainable way.
18+
- [Faker](https://faker.readthedocs.io/en/master/) – Generates fake data for testing purposes.
19+
- [Pytest](https://docs.pytest.org/en/stable/) – A full-featured and powerful testing framework in Python.
20+
- [JSON Schema validation](https://python-jsonschema.readthedocs.io/en/stable/) – Ensures contract testing by validating
21+
API responses against predefined schemas.
22+
- [Allure](https://allurereport.org/) – A comprehensive test reporting framework that provides detailed and visually
23+
appealing reports.
24+
- [Assertions in separate functions](./tools/assertions) – Encourages reusable and atomic test assertions.
25+
- [API client abstraction](./clients/operations_client.py) – Encapsulates API interaction logic for better modularity
26+
and reusability.
27+
- [Base API client](./clients/base_client.py) – Serves as the entry point for all HTTP calls.
28+
- [Logging features](./tools/logger.py) – Improves test readability and debugging, especially in CI/CD environments.
29+
- [Routing as Enum](./tools/routes.py) – Replaces raw string URLs with an enumerated routing system for better
30+
maintainability.
31+
- [Pytest plugins](./fixtures) – Moves fixtures out of conftest.py to keep the test suite organized and manageable.
32+
- [HTTPX event hooks](./clients/event_hooks.py) – Enables logging and additional features without modifying the original
33+
API client code.
34+
35+
This project targets[SampleAPIs](https://sampleapis.com/), which provides various fake API datasets for testing.
36+
Specifically, this project works with the [FakeBank API](https://sampleapis.com/api-list/fakebank) and uses its
37+
endpoints for testing purposes.
38+
39+
## Setup Instructions
40+
41+
### Prerequisites
42+
43+
Ensure that you have the following installed on your system:
44+
45+
- Python 3.11 or later
46+
- pip (Python package manager)
47+
- Git
48+
49+
## Installation
50+
51+
Clone the repository and navigate to the project directory:
52+
53+
```shell
54+
git clone https://github.com/Nikita-Filonov/python-api-tests.git
55+
cd python-api-tests
56+
```
57+
58+
Create and activate a virtual environment:
59+
60+
```shell
61+
python -m venv venv # Create virtual environment
62+
source venv/bin/activate # Activate on macOS/Linux
63+
venv\Scripts\activate # Activate on Windows
64+
```
65+
66+
Install dependencies:
67+
68+
```shell
69+
pip install --upgrade pip # Upgrade pip to the latest version
70+
pip install -r requirements.txt # Install required dependencies
71+
```
72+
73+
## Running Tests
74+
75+
To run API tests using pytest:
76+
77+
```shell
78+
pytest -m regression --numprocesses 2 # Run regression tests in parallel
79+
```
80+
81+
## Generating Allure Reports
82+
83+
Run tests and generate Allure results:
84+
85+
```shell
86+
pytest -m regression --alluredir=allure-results
87+
```
88+
89+
To serve the Allure report locally:
90+
91+
```shell
92+
allure serve allure-results
93+
```
94+
95+
## Running Tests in CI/CD
96+
97+
Tests are automatically executed in a CI/CD pipeline using [GitHub Actions](https://github.com/features/actions). The
98+
workflow is configured to:
99+
100+
- Run tests on every push and pull request to the main branch.
101+
- Generate and upload Allure reports as artifacts.
102+
- Publish the[ Allure report](https://allurereport.org/) to [GitHub Pages](https://pages.github.com/) for easy access.
103+
104+
Ensure that the [gh-pages](https://github.com/Nikita-Filonov/python-api-tests/tree/gh-pages) branch exists in your
105+
repository for successful deployment. If it does not exist, create it manually:
106+
107+
```shell
108+
git checkout --orphan gh-pages
109+
```
110+
111+
Then push the new branch:
112+
113+
```shell
114+
git push origin gh-pages
115+
```
116+
117+
To allow GitHub Actions to publish the report, enable Workflow permissions:
118+
119+
- Open your repository on GitHub.
120+
- Go to Settings > Actions > General.
121+
- Scroll down to Workflow permissions.
122+
- Select Read and write permissions.
123+
- Click Save.
124+
125+
Once set up, your tests will run automatically, and the Allure report will be deployed to GitHub Pages.
126+
127+
## Accessing Allure Reports
128+
129+
After a successful test run in CI/CD:
130+
131+
- The Allure report will be available at [GitHub Pages](https://nikita-filonov.github.io/python-api-tests/8/index.html).
132+
- The workflow logs and artifacts can be accessed
133+
via [GitHub Actions](https://github.com/Nikita-Filonov/python-api-tests/actions).
134+
- If the [*pages build and deployment*](https://github.com/Nikita-Filonov/python-api-tests/actions/runs/14155385792)
135+
workflow does not appear, verify your GitHub Pages settings:
136+
- Go to Settings > Pages.
137+
- Under Build and deployment, ensure the source is set to the `gh-pages` branch.
138+
139+
## Documentation for Used Actions
140+
141+
For detailed information on GitHub Actions used in this project, refer to the following:
142+
143+
- [Checkout Action](https://github.com/actions/checkout)
144+
- [Setup Python Action](https://github.com/actions/setup-python)
145+
- [Upload Artifact Action](https://github.com/actions/upload-artifact)
146+
- [Download Artifact Action](https://github.com/actions/download-artifact)
147+
- [Allure Report Action](https://github.com/simple-elf/allure-report-action)
148+
- [GitHub Pages Deployment Action](https://github.com/peaceiris/actions-gh-pages)
149+
150+
## Summary
151+
152+
This project serves as a reference implementation for writing clean, maintainable, and effective API tests in Python. It
153+
showcases best practices such as using a dedicated API client, structured assertions, logging, and CI/CD integration
154+
with Allure reporting. The setup allows for easy test execution locally and in CI/CD environments, ensuring high-quality
155+
automated testing with minimal manual intervention.
156+

tools/assertions/schema.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@
1111

1212
@allure.step("Validating JSON schema")
1313
def validate_json_schema(instance: Any, schema: dict) -> None:
14-
"""
15-
Проверяет, соответствует ли JSON-объект (instance) заданной JSON-схеме (schema).
16-
17-
:param instance: JSON-данные, которые нужно проверить.
18-
:param schema: Ожидаемая JSON-schema.
19-
:raises jsonschema.exceptions.ValidationError: Если instance не соответствует schema.
20-
"""
2114
logger.info("Validating JSON schema")
2215

2316
validate(

0 commit comments

Comments
 (0)