-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_main.py
40 lines (33 loc) · 1.31 KB
/
test_main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from fastapi.testclient import TestClient
from main import app
client = TestClient(app)
def test_root():
# Testing the root endpoint
response = client.get("/")
assert response.status_code == 200
assert response.json() == {"message": "Welcome to the Math API"}
def test_add():
# Testing the addition operation
response = client.post("/add", json={"a": 5, "b": 3})
assert response.status_code == 200
assert response.json() == {"result": 8}
def test_subtract():
# Testing the subtraction operation
response = client.post("/subtract", json={"a": 10, "b": 4})
assert response.status_code == 200
assert response.json() == {"result": 6}
def test_multiply():
# Testing the multiplication operation
response = client.post("/multiply", json={"a": 7, "b": 6})
assert response.status_code == 200
assert response.json() == {"result": 42}
def test_divide_success():
# Testing the division operation (success)
response = client.post("/divide", json={"a": 20, "b": 5})
assert response.status_code == 200
assert response.json() == {"result": 4}
def test_divide_by_zero():
# Testing division by zero
response = client.post("/divide", json={"a": 10, "b": 0})
assert response.status_code == 400
assert response.json() == {"detail": "Cannot divide by zero."}