-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApiTestPetStore_WithoutReport.py
99 lines (83 loc) · 2.96 KB
/
ApiTestPetStore_WithoutReport.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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import requests
import json
def uploadImagePet(petID):
# URL API
url = f'https://petstore.swagger.io/v2/pet/{petID}/uploadImage'
# پارامترهای درخواست
headers = {
'accept': 'application/json',
}
# فایل برای آپلود
files = {
'file': ('PetDogs.jpg', open('Document/UploadFile/PetDogs.jpg', 'rb'), 'image/jpeg')
}
print("=============Start Test 1=================")
# ارسال درخواست POST
response = requests.post(url, headers=headers, files=files)
print(f'Status Code: {response.status_code}')
printResponse1 = input("Do you want the response to be printed? (Y/N): ")
if printResponse1 == 'Y':
# نمایش وضعیت و پاسخ سرور
print(f'Status Code: {response.status_code}')
try:
print(f'Response JSON: {response.json()}')
except json.JSONDecodeError:
print("Response content is not in JSON format")
print(f'Response Headers: {response.headers}')
if response.status_code == 200:
print('Test "Add Image To Pet" : PASS')
else:
print(f'Status Code: {response.status_code}')
print('Add Image to pet Fail : X')
print("==============End Test 1===================")
def addPet():
urlAddPet = 'https://petstore.swagger.io/v2/pet'
body = {
"id": f'{petID}',
"category": {
"id": 1,
"name": "dogs"
},
"name": "jimmyPet",
"photoUrls": [
"./PetDogs.jpg"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
}
print("=============Start Test 2==================")
response = requests.post(urlAddPet, headers=headers, data=json.dumps(body))
print(f'Status Code: {response.status_code}')
printResponse = input("Do you want the response to be printed? (Y/N): ")
if printResponse == "Y":
try:
assert response.status_code == 200
print(f"Status Code : {response.status_code}")
print(f"Response JSON : {response.json()}")
print(f"Response Headers : {response.headers}")
except AssertionError:
print(f"Status Code : {response.status_code}")
print("Test Fail : X")
except json.JSONDecodeError:
print("Response content is not in JSON format")
print(f"Response Text : {response.text}")
print(f"Response Headers : {response.headers}")
if response.status_code == 200:
print('Test "Add Pet" : PASS')
else:
print('Test "Add Pet" : FAIL')
print("==============End Test 2===================")
# فراخوانی توابع برای تست
if __name__ == "__main__":
petID = int(input("Enter your pet ID: "))
uploadImagePet(petID)
addPet()