API Testing with Python (Requests Module).

What is API Testing?

API testing is a type of software testing which involves testing application programming interface or software resource as part of product integration testing to determine if they meet expectations or functionality, reliability, performance, and security.

What are types of API?

Simple Object Access Protocol (SOAP)

Remote Procedure Call (RPC)

Representational State Transfer (REST)

Out of above three, last one has become now industry standard API implementation with many feature ready standards and also it has support for many programming languages.

Python Request Module for testing API

Request is a Python Module for testing application programming interface with features of get , post, put and delete.

The Requests module is a an elegant and simple HTTP library for Python.

Simple Example:-

Make a Request


import requests

r = requests. get (‘https://api.github.com/events’)

r = requests.post (‘https: //httpbin.org/post’, data = {‘key’:’value’})

r = requests. put (‘https://httpbin.org/put’, data = {‘key’:’value’})

r = requests. delete (‘https://httpbin.org/delete’)

r = requests. head (‘https://httpbin.org/get’)

r = requests. options (‘https://httpbin.org/get’)

r = requests. options (‘https://httpbin.org/get’)

# Passing Parameters in URL

payload ={‘key1’:’value1’,’key2’:’value2’}

r = requests. get (‘https://api.github.com/events’, params=payload)

# Response Content

r = requests. get (‘https://api.github.com/events’)

r.text

[{u'repository': {u'open_issues': 0, u'url': 'https://github.com.....]

r.encoding

utf-8

r.encoding = ‘ISO-8859-1’

# JSON Response Content

r = requests. get (‘https://api.github.com/events’)

r.json()

[{u'repository': {u'open_issues': 0, u'url': 'https://github.com.....]

For Complete Requests Module  Reference, Check the below links.

http://docs.python-requests.org/en/master/

https://pypi.python.org/pypi/requests