Skip to content

Commit 8dfb614

Browse files
committed
feat(tests): add fixture with TestClient definition
1 parent 31a84e5 commit 8dfb614

File tree

2 files changed

+29
-23
lines changed

2 files changed

+29
-23
lines changed

tests/conftest.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pytest
2+
import warnings
3+
from fastapi.testclient import TestClient
4+
from main import app
5+
6+
# Suppress the DeprecationWarning from httpx
7+
warnings.filterwarnings("ignore", category=DeprecationWarning)
8+
9+
10+
@pytest.fixture(scope="module")
11+
def client():
12+
with TestClient(app) as client:
13+
yield client

tests/test_main.py

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,18 @@
44

55
import json
66
from tests.player_stub import existing_player, non_existing_player, unknown_player
7-
from main import app
8-
from fastapi.testclient import TestClient
9-
import warnings
10-
# Suppress the DeprecationWarning from httpx
11-
warnings.filterwarnings("ignore", category=DeprecationWarning)
127

13-
14-
client = TestClient(app)
158
PATH = "/players/"
169

1710
# GET /players/ ----------------------------------------------------------------
1811

1912

20-
def test_given_get_when_request_path_has_no_id_then_response_status_code_should_be_200_ok():
13+
def test_given_get_when_request_path_has_no_id_then_response_status_code_should_be_200_ok(client):
2114
response = client.get(PATH)
2215
assert response.status_code == 200
2316

2417

25-
def test_given_get_when_request_path_has_no_id_then_response_body_should_be_collection_of_players():
18+
def test_given_get_when_request_path_has_no_id_then_response_body_should_be_collection_of_players(client):
2619
response = client.get(PATH)
2720
players = response.json()
2821
player_id = 0
@@ -33,19 +26,19 @@ def test_given_get_when_request_path_has_no_id_then_response_body_should_be_coll
3326
# GET /players/{player_id} -----------------------------------------------------
3427

3528

36-
def test_given_get_when_request_path_is_non_existing_id_then_response_status_code_should_be_404_not_found():
29+
def test_given_get_when_request_path_is_non_existing_id_then_response_status_code_should_be_404_not_found(client):
3730
player_id = 999
3831
response = client.get(PATH + str(player_id))
3932
assert response.status_code == 404
4033

4134

42-
def test_given_get_when_request_path_is_existing_id_then_response_status_code_should_be_200_ok():
35+
def test_given_get_when_request_path_is_existing_id_then_response_status_code_should_be_200_ok(client):
4336
player_id = 1
4437
response = client.get(PATH + str(player_id))
4538
assert response.status_code == 200
4639

4740

48-
def test_given_get_when_request_path_is_existing_id_then_response_body_should_be_matching_player():
41+
def test_given_get_when_request_path_is_existing_id_then_response_body_should_be_matching_player(client):
4942
player_id = 1
5043
response = client.get(PATH + str(player_id))
5144
player = response.json()
@@ -54,19 +47,19 @@ def test_given_get_when_request_path_is_existing_id_then_response_body_should_be
5447
# GET /players/squadnumber/{squad_number} --------------------------------------
5548

5649

57-
def test_given_get_when_request_path_is_non_existing_squad_number_then_response_status_code_should_be_404_not_found():
50+
def test_given_get_when_request_path_is_non_existing_squad_number_then_response_status_code_should_be_404_not_found(client):
5851
squad_number = 999
5952
response = client.get(PATH + "squadnumber" + "/" + str(squad_number))
6053
assert response.status_code == 404
6154

6255

63-
def test_given_get_when_request_path_is_existing_squad_number_then_response_status_code_should_be_200_ok():
56+
def test_given_get_when_request_path_is_existing_squad_number_then_response_status_code_should_be_200_ok(client):
6457
squad_number = 10
6558
response = client.get(PATH + "squadnumber" + "/" + str(squad_number))
6659
assert response.status_code == 200
6760

6861

69-
def test_given_get_when_request_path_is_existing_squad_number_then_response_body_should_be_matching_player():
62+
def test_given_get_when_request_path_is_existing_squad_number_then_response_body_should_be_matching_player(client):
7063
squad_number = 10
7164
response = client.get(PATH + "squadnumber" + "/" + str(squad_number))
7265
player = response.json()
@@ -75,20 +68,20 @@ def test_given_get_when_request_path_is_existing_squad_number_then_response_body
7568
# POST /players/ ---------------------------------------------------------------
7669

7770

78-
def test_given_post_when_request_body_is_empty_then_response_status_code_should_be_422_unprocessable_entity():
71+
def test_given_post_when_request_body_is_empty_then_response_status_code_should_be_422_unprocessable_entity(client):
7972
body = json.dumps({})
8073
response = client.post(PATH, data=body)
8174
assert response.status_code == 422
8275

8376

84-
def test_given_post_when_request_body_is_existing_player_then_response_status_code_should_be_409_conflict():
77+
def test_given_post_when_request_body_is_existing_player_then_response_status_code_should_be_409_conflict(client):
8578
player = existing_player()
8679
body = json.dumps(player.__dict__)
8780
response = client.post(PATH, data=body)
8881
assert response.status_code == 409
8982

9083

91-
def test_given_post_when_request_body_is_non_existing_player_then_response_status_code_should_be_201_created():
84+
def test_given_post_when_request_body_is_non_existing_player_then_response_status_code_should_be_201_created(client):
9285
player = non_existing_player()
9386
body = json.dumps(player.__dict__)
9487
response = client.post(PATH, data=body)
@@ -97,22 +90,22 @@ def test_given_post_when_request_body_is_non_existing_player_then_response_statu
9790
# PUT /players/{player_id} -----------------------------------------------------
9891

9992

100-
def test_given_put_when_request_body_is_empty_then_response_status_code_should_be_422_unprocessable_entity():
93+
def test_given_put_when_request_body_is_empty_then_response_status_code_should_be_422_unprocessable_entity(client):
10194
player_id = 1
10295
body = {}
10396
response = client.put(PATH + str(player_id), data=body)
10497
assert response.status_code == 422
10598

10699

107-
def test_given_put_when_request_body_is_unknown_player_then_response_status_code_should_be_404_not_found():
100+
def test_given_put_when_request_body_is_unknown_player_then_response_status_code_should_be_404_not_found(client):
108101
player_id = 999
109102
player = unknown_player()
110103
body = json.dumps(player.__dict__)
111104
response = client.put(PATH + str(player_id), data=body)
112105
assert response.status_code == 404
113106

114107

115-
def test_given_put_when_request_body_is_existing_player_then_response_status_code_should_be_204_no_content():
108+
def test_given_put_when_request_body_is_existing_player_then_response_status_code_should_be_204_no_content(client):
116109
player_id = 1
117110
player = existing_player()
118111
player.first_name = "Emiliano"
@@ -124,13 +117,13 @@ def test_given_put_when_request_body_is_existing_player_then_response_status_cod
124117
# DELETE /players/{player_id} --------------------------------------------------
125118

126119

127-
def test_given_delete_when_request_path_is_non_existing_id_then_response_status_code_should_be_404_not_found():
120+
def test_given_delete_when_request_path_is_non_existing_id_then_response_status_code_should_be_404_not_found(client):
128121
player_id = 999
129122
response = client.delete(PATH + str(player_id))
130123
assert response.status_code == 404
131124

132125

133-
def test_given_delete_when_request_path_is_existing_id_then_response_status_code_should_be__204_no_content():
126+
def test_given_delete_when_request_path_is_existing_id_then_response_status_code_should_be__204_no_content(client):
134127
player_id = 12
135128
response = client.delete(PATH + str(player_id))
136129
assert response.status_code == 204

0 commit comments

Comments
 (0)