Back to list
ak-eyther

pytest-backend-patterns

by ak-eyther

A project to copy paste the entire settings

0🍴 0📅 Jan 6, 2026

SKILL.md


name: pytest-backend-patterns description: Backend testing with pytest including fixtures, mocking, async tests, and database testing. Use for FastAPI/Python test implementation. allowed-tools:

  • Read
  • Write
  • Edit
  • Bash

Pytest Backend Patterns

Basic Test

import pytest
from fastapi.testclient import TestClient
from app.main import app

client = TestClient(app)

def test_read_user():
    response = client.get("/users/1")
    assert response.status_code == 200
    assert response.json()["id"] == 1

Fixtures

@pytest.fixture
def db_session():
    session = SessionLocal()
    try:
        yield session
    finally:
        session.close()

@pytest.fixture
def test_user(db_session):
    user = User(email="test@example.com")
    db_session.add(user)
    db_session.commit()
    return user

def test_get_user(test_user):
    response = client.get(f"/users/{test_user.id}")
    assert response.json()["email"] == "test@example.com"

Async Tests

import pytest
from httpx import AsyncClient

@pytest.mark.asyncio
async def test_async_endpoint():
    async with AsyncClient(app=app, base_url="http://test") as ac:
        response = await ac.get("/users/1")
    assert response.status_code == 200

Database Test Isolation

@pytest.fixture(scope="function")
def db_session():
    connection = engine.connect()
    transaction = connection.begin()
    session = Session(bind=connection)
    
    yield session
    
    session.close()
    transaction.rollback()
    connection.close()

Mocking External APIs

from unittest.mock import patch, MagicMock

@patch('app.services.external_api.fetch_data')
def test_external_api_call(mock_fetch):
    mock_fetch.return_value = {"data": "mocked"}
    
    response = client.get("/external-data")
    assert response.json() == {"data": "mocked"}
    mock_fetch.assert_called_once()

Parametrized Tests

@pytest.mark.parametrize("email,expected_valid", [
    ("test@example.com", True),
    ("invalid-email", False),
    ("@example.com", False),
])
def test_email_validation(email, expected_valid):
    is_valid = validate_email(email)
    assert is_valid == expected_valid

Score

Total Score

60/100

Based on repository quality metrics

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

3ヶ月以内に更新がある

0/10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

0/5

Reviews

💬

Reviews coming soon