FastAPI स्वचालित परीक्षण

FastAPI परिचय

Matt Eckerle

Software and Data Engineering Leader

स्वचालित टेस्ट क्या हैं?

यूनिट टेस्ट

  • फोकस: पृथक कोड
  • उद्देश्य: कोड फ़ंक्शन सत्यापित करें
  • दायरा: फ़ंक्शन या मेथड
  • पर्यावरण: पृथक Python एन्व
def test_main():
    response = main()
    assert response == {"msg": "Hello"}

सिस्टम टेस्ट

  • फोकस: पृथक सिस्टम ऑपरेशंस
  • उद्देश्य: सिस्टम फ़ंक्शन सत्यापित करें
  • दायरा: एंडपॉइंट
  • पर्यावरण: चल रहे ऐप के साथ Python एन्व
def test_read_main():
    response = client.get("/")
    assert response.status_code == 200
    assert response.json() == {"msg": 
                               "Hello"}
FastAPI परिचय

TestClient का उपयोग

TestClient: pytest के लिए HTTP क्लाइंट

# TestClient और ऐप इंपोर्ट करें
from fastapi.testclient import TestClient
from .main import app

# ऐप कॉन्टेक्स्ट के साथ टेस्ट क्लाइंट बनाएँ
client = TestClient(app)

def test_main(): response = client.get("/") assert response.status_code == 200 assert response.json() == {"msg": "Hello"}
FastAPI परिचय

त्रुटि या विफलता प्रतिक्रियाओं का परीक्षण

ऐप

app = FastAPI()

@app.delete("/items")
def delete_item(item: Item):
    if item.id not in item_ids:
        raise HTTPException(
            status_code=404, 
            detail="Item not found.")
    else:
        delete_item_in_database(item)
        return {}

टेस्ट

def test_delete_nonexistent_item():
    response = client.delete(
        "/items",
        json={"id": -999})
    assert response.status_code == 404
    json = response.json()
    assert json == {"detail":
                    "Item not found."}
FastAPI परिचय

Let's practice!

FastAPI परिचय

Preparing Video For Download...