forked from automationExamples/pytest-api-example
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_pet.py
More file actions
70 lines (58 loc) · 2.31 KB
/
test_pet.py
File metadata and controls
70 lines (58 loc) · 2.31 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from jsonschema import validate
import pytest
import schemas
import api_helpers
from hamcrest import assert_that, contains_string, is_
'''
TODO: Finish this test by...
1) Troubleshooting and fixing the test failure
The purpose of this test is to validate the response matches the expected schema defined in schemas.py
'''
def test_pet_schema():
test_endpoint = "/pets/1"
response = api_helpers.get_api_data(test_endpoint)
assert response.status_code == 200
# Validate the response schema against the defined schema in schemas.py
validate(instance=response.json(), schema=schemas.pet)
# Verify the response contains expected data
data = response.json()
assert data['id'] == 1
assert data['name'] == 'ranger'
assert data['type'] == 'dog'
assert data['status'] == 'pending'
'''
TODO: Finish this test by...
1) Extending the parameterization to include all available statuses
2) Validate the appropriate response code
3) Validate the 'status' property in the response is equal to the expected status
4) Validate the schema for each object in the response
'''
@pytest.mark.parametrize("status", ["available", "sold", "pending"])
def test_find_by_status_200(status):
test_endpoint = "/pets/findByStatus"
params = {
"status": status
}
response = api_helpers.get_api_data(test_endpoint, params)
# Validate the response code
assert response.status_code == 200
# Get response data
pets_list = response.json()
# Validate that all returned pets have the expected status
for pet in pets_list:
assert pet['status'] == status, f"Expected status '{status}' but got '{pet['status']}'"
# Validate the schema for each object in the response
validate(instance=pet, schema=schemas.pet)
'''
TODO: Finish this test by...
1) Testing and validating the appropriate 404 response for /pets/{pet_id}
2) Parameterizing the test for any edge cases
'''
@pytest.mark.parametrize("pet_id", [999, -1, 1000000])
def test_get_by_id_404(pet_id):
test_endpoint = f"/pets/{pet_id}"
response = api_helpers.get_api_data(test_endpoint)
# Validate the 404 response code
assert response.status_code == 404
# Validate the error message contains appropriate text
assert_that(response.text, contains_string(f"Pet with ID {pet_id} not found"))