-
Notifications
You must be signed in to change notification settings - Fork 190
/
test_api_example.py
154 lines (127 loc) · 6.82 KB
/
test_api_example.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
"""
API EXAMPLE TEST
1. Add new car - POST request(without url_params)
2. Get all cars - GET request(without url_params)
3. Verify car count
4. Update newly added car details -PUT request
5. Get car details -GET request(with url_params)
6. Register car - POST request(with url_params)
7. Get list of registered cars -GET
8. Verify registered cars count
9. Delete newly added car -DELETE request
"""
import os
import sys
import pytest
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from conf import api_example_conf as conf
from conf import base_url_conf
from conftest import interactivemode_flag
@pytest.mark.API
def test_api_example(test_api_obj):
"Run api test"
try:
expected_pass = 0
actual_pass = -1
# set authentication details
username = conf.user_name
password = conf.password
auth_details = test_api_obj.set_auth_details(username, password)
initial_car_count = test_api_obj.get_car_count(auth_details)
# add cars
car_details = conf.car_details
result_flag = test_api_obj.add_car(car_details=car_details,
auth_details=auth_details)
test_api_obj.log_result(result_flag,
positive='Successfully added new car with details %s' % car_details,
negative='Could not add new car with details %s' % car_details)
# Get Cars and verify if new car is added
result_flag = test_api_obj.get_cars(auth_details)
result_flag = test_api_obj.verify_car_count(expected_count=initial_car_count+1,
auth_details=auth_details)
test_api_obj.log_result(result_flag,
positive='Total car count matches expected count',
negative='Total car count doesnt match expected count')
# Update car
update_car = conf.update_car
update_car_name = conf.car_name_2
result_flag = test_api_obj.update_car(auth_details=auth_details,
car_name=update_car_name,
car_details=update_car)
test_api_obj.log_result(result_flag,
positive='Successfully updated car : %s' % update_car_name,
negative='Couldnt update car :%s' % update_car_name)
# Get one car details
new_car = conf.car_name_1
brand = conf.brand
result_flag = test_api_obj.get_car(auth_details=auth_details,
car_name=new_car,
brand=brand)
test_api_obj.log_result(result_flag,
positive='Successfully fetched car details of car : %s' % new_car,
negative='Couldnt fetch car details of car :%s' % new_car)
# Register car
customer_details = conf.customer_details
result_flag = test_api_obj.register_car(auth_details=auth_details,
car_name=new_car,
brand=brand)
test_api_obj.log_result(result_flag,
positive='Successfully registered new car %s with customer details %s' % (new_car, customer_details),
negative='Couldnt register new car %s with cutomer details %s' % (new_car, customer_details))
#Get Registered cars and check count
result_flag = test_api_obj.get_registered_cars(auth_details)
register_car_count = test_api_obj.get_regi_car_count(auth_details)
result_flag = test_api_obj.verify_registration_count(expected_count=register_car_count,
auth_details=auth_details)
test_api_obj.log_result(result_flag,
positive='Registered count matches expected value',
negative='Registered car count doesnt match expected value')
# Remove newly added car
result_flag = test_api_obj.remove_car(auth_details=auth_details,
car_name=update_car_name)
test_api_obj.log_result(result_flag,
positive='Successfully deleted car %s' % update_car,
negative='Could not delete car %s ' % update_car)
# validate if car is deleted
result_flag = test_api_obj.verify_car_count(expected_count=initial_car_count,
auth_details=auth_details)
test_api_obj.log_result(result_flag,
positive='Total car count matches expected count after deleting one car',
negative='Total car count doesnt match expected count after deleting one car')
# Deleting registered car
test_api_obj.delete_registered_car(auth_details)
# test for validation http error 403
result = test_api_obj.check_validation_error(auth_details)
test_api_obj.log_result(not result['result_flag'],
positive=result['msg'],
negative=result['msg'])
# test for validation http error 401 when no authentication
auth_details = None
result = test_api_obj.check_validation_error(auth_details)
test_api_obj.log_result(not result['result_flag'],
positive=result['msg'],
negative=result['msg'])
# test for validation http error 401 for invalid authentication
# set invalid authentication details
username = conf.invalid_user_name
password = conf.invalid_password
auth_details = test_api_obj.set_auth_details(username, password)
result = test_api_obj.check_validation_error(auth_details)
test_api_obj.log_result(not result['result_flag'],
positive=result['msg'],
negative=result['msg'])
# write out test summary
expected_pass = test_api_obj.total
actual_pass = test_api_obj.passed
test_api_obj.write_test_summary()
except Exception as e:
print(e)
if base_url_conf.api_base_url == 'http://127.0.0.1:5000':
test_api_obj.write("Please run the test against https://cars-app.qxf2.com/ by changing the api_base_url in base_url_conf.py")
test_api_obj.write("OR")
test_api_obj.write("Clone the repo 'https://github.com/qxf2/cars-api.git' and run the cars_app inorder to run the test against your system")
else:
test_api_obj.write("Exception when trying to run test:%s" % __file__)
test_api_obj.write("Python says:%s" % str(e))
# Assertion
assert expected_pass == actual_pass,"Test failed: %s"%__file__