Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/themusharraf/testing-fastapi
Unit and Integration tests for Apis
https://github.com/themusharraf/testing-fastapi
api fastapi integration-testing unit-testing
Last synced: 2 months ago
JSON representation
Unit and Integration tests for Apis
- Host: GitHub
- URL: https://github.com/themusharraf/testing-fastapi
- Owner: themusharraf
- Created: 2024-11-16T17:29:03.000Z (2 months ago)
- Default Branch: main
- Last Pushed: 2024-11-16T17:53:02.000Z (2 months ago)
- Last Synced: 2024-11-16T18:25:52.586Z (2 months ago)
- Topics: api, fastapi, integration-testing, unit-testing
- Language: Python
- Homepage:
- Size: 0 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# π FastAPI Foydalanuvchi Boshqaruvi API - Unit va Integration Testlar
Ushbu loyiha **FastAPI** asosida foydalanuvchilarni boshqarish API uchun mo'ljallangan. Loyiha ichida foydalanuvchilarni qo'shish va olish funksiyalari mavjud bo'lib, ularga **Unit test** va **Integration testlar** orqali sinovlar yozilgan.
---
## π Xususiyatlar
- **Foydalanuvchi qo'shish** (POST so'rovi)
- **Foydalanuvchini olish** (GET so'rovi)
- **Unit testlar**: Foydalanuvchi yaratish va olishning asosiy logikasini izolyatsiyalangan holda sinash.
- **Integration testlar**: API endpointlarining to'g'ri ishlashini sinash.---
## π O'rnatish
1. Zarur kutubxonalarni o'rnatish:
```bash
pip install fastapi uvicorn pydantic pytest
```
2. βΆοΈ Ilovani Ishga Tushirish
```bash
uvicorn main:app --reload
```
Odatiy holda server http://127.0.0.1:8000/docs manzilida ishga tushadi.3. π§ Testlarni Ishga Tushirish
Testlarni ishga tushirish uchun quyidagilarni bajaring:
Unit va Integration testlarni bajarish:
```bash
pytest
```
Test natijalarini ko'rib chiqing. Hammasi muvaffaqiyatli o'tishi kerak.## π§ͺ Testlar Tafsiloti
### Integration Testlar
Integration testlar API endpointlarning ishlashini sinaydi. Quyidagi kod test_create_user_and_get funksiyasida API endpointlar orqali foydalanuvchini yaratish va olishni sinovdan o'tkazadi:
```python
def test_create_user_and_get():
# Yangi foydalanuvchi qo'shish
resp = client.post('/user/', json={"id": 4, "name": "Bekzod", "username": "bekzod", "email": "[email protected]"})
assert resp.status_code == 200
assert resp.json() == {"id": 4, "name": "Bekzod", "username": "bekzod", "email": "[email protected]"}# Foydalanuvchini olish
resp = client.get('/user/4')
assert resp.status_code == 200
assert resp.json() == {"id": 4, "name": "Bekzod", "username": "bekzod", "email": "[email protected]"}# Takroran bir xil foydalanuvchini qo'shishga urinish
resp = client.post('/user/', json={"id": 4, "name": "Bekzod", "username": "bekzod", "email": "[email protected]"})
assert resp.status_code == 400
assert resp.json() == {"detail": "User already exists"}# Mavjud bo'lmagan foydalanuvchini olish
resp = client.get('/user/5')
assert resp.status_code == 404
assert resp.json() == {"detail": "User not found"}
```
### Unit Testlar
Unit testlar kodning logikasini izolyatsiyalangan holda sinash uchun yozilgan. Quyidagi testlar foydalanuvchini yaratish (create_user) va olish (get_user) funksiyalarini sinaydi.
Foydalanuvchi yaratish testi
```python
def test_create_user():
# Yangi foydalanuvchi qo'shish
user = create_user(4, "Bekzod", "bekzod", "[email protected]")
assert user == {"id": 4, "name": "Bekzod", "username": "bekzod", "email": "[email protected]"}
assert users[4] == user# Takroriy foydalanuvchini qo'shishga urinish
with pytest.raises(ValueError, match="User already exists"):
create_user(4, "Bekzod", "bekzod", "[email protected]")
```
Foydalanuvchini olish testi
```python
def test_get_user():
# Test uchun foydalanuvchi qo'shish
users[4] = {"id": 4, "name": "Bekzod", "username": "bekzod", "email": "[email protected]"}# Foydalanuvchini olish
user = get_user(4)
assert user == {"id": 4, "name": "Bekzod", "username": "bekzod", "email": "[email protected]"}# Mavjud bo'lmagan foydalanuvchini olishga urinish
with pytest.raises(ValueError, match="User not found"):
get_user(5)
```
## π Qoβshimcha Ma'lumotlar
- FastAPI hujjatlari: https://fastapi.tiangolo.com/
- Pytest hujjatlari: https://docs.pytest.org/
- Pydantic hujjatlari: https://pydantic-docs.helpmanual.io/## π Hissa Qoβshish
Loyihani fork qilib, yaxshilanishlar kiriting va pull request yuboring. Sizning hissangiz qadrlanadi! π