@@ -44,14 +44,10 @@ In ``conftest.py``, create fixtures for your test database:
4444 # Use in-memory SQLite for tests
4545 SQLALCHEMY_DATABASE_URL = " sqlite:///./test.db"
4646 engine = create_engine(
47- SQLALCHEMY_DATABASE_URL ,
48- connect_args = {" check_same_thread" : False }
49- )
50- TestingSessionLocal = sessionmaker(
51- autocommit = False ,
52- autoflush = False ,
53- bind = engine
47+ SQLALCHEMY_DATABASE_URL , connect_args = {" check_same_thread" : False }
5448 )
49+ TestingSessionLocal = sessionmaker(autocommit = False , autoflush = False , bind = engine)
50+
5551
5652 @pytest.fixture (scope = " function" )
5753 def db ():
@@ -63,6 +59,7 @@ In ``conftest.py``, create fixtures for your test database:
6359 db.close()
6460 Base.metadata.drop_all(bind = engine)
6561
62+
6663 @pytest.fixture (scope = " function" )
6764 def client (db ):
6865 def override_get_db ():
@@ -86,21 +83,23 @@ In ``test_api.py``, create your test cases:
8683 import pytest
8784 from app.auth import get_password_hash
8885
86+
8987 def test_register_user (client , db ):
9088 response = client.post(
9189 " /register" ,
9290 json = {
9391 " username" : " testuser" ,
9492 " password" : " testpassword" ,
959396- " full_name" : " Test User"
97- }
94+ " full_name" : " Test User" ,
95+ },
9896 )
9997 assert response.status_code == 200
10098 data = response.json()
10199 assert data[" username" ] == " testuser"
102100 assert " id" in data
103101
102+
104103 def test_login_user (client , db ):
105104 # First create a user
106105 hashed_password = get_password_hash(" testpassword" )
@@ -110,17 +109,13 @@ In ``test_api.py``, create your test cases:
110109 " username" : " testuser" ,
111110 " password" : " testpassword" ,
112111113- " full_name" : " Test User"
114- }
112+ " full_name" : " Test User" ,
113+ },
115114 )
116115
117116 # Then attempt login
118117 response = client.post(
119- " /token" ,
120- data = {
121- " username" : " testuser" ,
122- " password" : " testpassword"
123- }
118+ " /token" , data = {" username" : " testuser" , " password" : " testpassword" }
124119 )
125120 assert response.status_code == 200
126121 assert " access_token" in response.json()
@@ -141,24 +136,19 @@ For endpoints that require authentication:
141136 " username" : " testuser" ,
142137 " password" : " testpassword" ,
143138144- " full_name" : " Test User"
145- }
139+ " full_name" : " Test User" ,
140+ },
146141 )
147142
148143 response = client.post(
149- " /token" ,
150- data = {
151- " username" : " testuser" ,
152- " password" : " testpassword"
153- }
144+ " /token" , data = {" username" : " testuser" , " password" : " testpassword" }
154145 )
155146
156147 access_token = response.json()[" access_token" ]
157- client.headers = {
158- " Authorization" : f " Bearer { access_token} "
159- }
148+ client.headers = {" Authorization" : f " Bearer { access_token} " }
160149 return client
161150
151+
162152 def test_protected_route (authorized_client ):
163153 response = authorized_client.get(" /protected-resource" )
164154 assert response.status_code == 200
@@ -178,11 +168,7 @@ Best Practices
178168
179169 def test_login_invalid_credentials (client ):
180170 response = client.post(
181- " /token" ,
182- data = {
183- " username" : " nonexistent" ,
184- " password" : " wrong"
185- }
171+ " /token" , data = {" username" : " nonexistent" , " password" : " wrong" }
186172 )
187173 assert response.status_code == 401
188174
@@ -204,9 +190,7 @@ Testing File Uploads
204190.. code-block :: python
205191
206192 def test_file_upload (client ):
207- files = {
208- " file" : (" test.txt" , b " test content" , " text/plain" )
209- }
193+ files = {" file" : (" test.txt" , b " test content" , " text/plain" )}
210194 response = client.post(" /upload" , files = files)
211195 assert response.status_code == 200
212196
@@ -218,6 +202,7 @@ Testing WebSocket Endpoints
218202 from fastapi.testclient import TestClient
219203 from fastapi.websockets import WebSocket
220204
205+
221206 def test_websocket_endpoint (client ):
222207 with client.websocket_connect(" /ws" ) as websocket:
223208 websocket.send_text(" Hello" )
0 commit comments