-
Notifications
You must be signed in to change notification settings - Fork 191
API Tests Quick Start Guide
For Player-Interface layer based API Automation Framework, we have written a sample API application called cars_app.py using Python Flask. In cars_app.py application we have features related to getting car details, adding cars, updating cars, removing cars, registering cars, filtering cars etc. We can make the required REST (GET, POST, PUT, DELETE) API calls to use the features.
Note
The Player-Interface API Automation Framework now supports Async
test scenario execution
API automation offers a sweet spot between unit testing and GUI automation. Most web applications these days have RESTful interfaces - lending themselves nicely to API automation. At Qxf2, we use the API automation to perform a lot of the test setup activities. API automation is fast, robust and much easier to maintain than GUI automation. The API automation framework is based on object-oriented approach and easier to maintain. To get setup with API and run your tests you need to do the following things.
To get ready with API test Framework, follow the steps as below:
- Get Cars API code from repository https://github.com/qxf2/cars-api. This can be achieved with command as below: "git clone https://github.com/qxf2/cars-api.git"
- Install Flask (web framework) as it is needed to run our cars-api web server using command "pip install flask"
- Before running the tests, Cars app server should be run in the background with command:"python cars-api/cars_app.py".
For more information related to API testing, you can refer to the links below: https://qxf2.com/blog/easily-maintainable-api-test-automation-framework/ https://qxf2.com/blog/api-testing-developer-tools/ https://qxf2.com/blog/api-testing-python-mechanize/
Here is the sample test script for writing a test for the Cars app we discussed earlier.
"""
API Test Cases to Player-Interface pattern based Framework will do the following:
# Test Case for Adding new car details
# Test Case to Update newly added car details
"""
import os,sys,time
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from endpoints.API_Player import API_Player
from conf import api_example_conf as conf
def test_api_example(api_url='http://127.0.0.1:5000'):
"Run api test"
try:
# Create a test object
test_obj = API_Player(url=api_url)
expected_pass = 0
actual_pass = -1
# set authentication details
username = conf.user_name
password = conf.password
auth_details = test_obj.set_auth_details(username, password)
# add cars
car_details = conf.car_details
result_flag = test_obj.add_car(car_details=car_details,
auth_details=auth_details)
test_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)
# Update car
update_car = conf.update_car
update_car_name = conf.car_name_2
result_flag = test_obj.update_car(auth_details=auth_details,
car_name=update_car_name,
car_details=update_car)
test_obj.log_result(result_flag,
positive='Successfully updated car : %s' % update_car_name,
negative='Couldnt update car :%s' % update_car_name)
# write out test summary
expected_pass = test_obj.total
actual_pass = test_obj.passed
test_obj.write_test_summary()
except Exception, e:
test_obj.write("Exception when trying to run test:%s" % __file__)
test_obj.write("Python says:%s" % str(e))
# Assertion
assert expected_pass == actual_pass
if __name__ == '__main__':
test_api_example()
Follow below steps to run test_api_example test:
- Start the cars application using command "python cars-api/cars_app.py".
- Run the test script "python -m pytest -k api -s"