Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Python REST API Unit Test

Using Python Unit Test and urllib2 modules, we can test REST API in test automation. OpenWeatherMap API provides current weather data and weather forecast by city name, city id, city geographic locations or city coordinates. In this example, we test the api using city name. Please remember that you need to obtain a new API Key in order to use the api for business or for large scale data retrieval. Selenium Master does not take any legal responsibilities if the API Key does not work for you. 

In our test script, we make the api call using urllib2 library and do assertion that the city name should exist in the response json string. 

Create a python file and save it as "SeleniumPythonRestApiUnitTest.py". Write the following scripts. 

import unittest
import json
import urllib2

class RestApiTest(unittest.TestCase):

    def setUp(self):
        #define Api URL and API Key
        self.ApiUrl = "http://api.openweathermap.org/data/2.5/weather"
        self.ApiKey="70926ddfd37fdf454548b8db13695995"
    def test_weather_api_by_city_name1(self):
        #define api response
        testurl=(self.ApiUrl+"?q=Baltimore,us"+"&"+"APPID="+self.ApiKey)
        print testurl
        response=urllib2.urlopen(testurl)
        #read response 
        html=response.read()
        #print response
        print(html)
        #assert response
        self.assertTrue("Baltimore" in html)

    def test_weather_api_by_city_name2(self):
        #define api response
        testurl=(self.ApiUrl+"?q=Baltimore,us"+"&"+"APPID="+self.ApiKey)
        print testurl
        response=urllib2.urlopen(testurl)
        #read response
        html=response.read()
        #print response
        print(html)
        #loads response as json
        json_data=json.loads(html)
        #get the key "name" value
        city_name=json_data["name"]
        print("city name is:"+city_name)
        #assert city name
        self.assertTrue(city_name=="Baltimore")

    def tearDown(self):
        print "------- test is over -------"
        
if __name__ == "__main__":
    unittest.main()

Run the above script with the command line below and observe the result on the console. The tests passed. I used "London,uk" and "Baltimore,us" in the api. You can change the city name and country to different names in your test practices.

C:\Python27>python SeleniumPythonRestApiUnitTest.py
http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=70926ddfd37fdf454548b8db13695995
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":310,"main":"Drizzle","description":"light intensity drizzle rain","icon":"09n"}],"base":"stations","main":{"temp":283.77,"pressure":1008,"humidity":87,"temp_min":282.15,"temp_max":284.45},"visibility":10000,"wind":{"speed":5.1,"deg":240},"clouds":{"all":75},"dt":1456114362,"sys":{"type":1,"id":5089,"message":0.0435,"country":"GB","sunrise":1456124483,"sunset":1456162053},"id":2643743,"name":"London","cod":200}
test is over
.http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=70926ddfd37fdf454548b8db13695995
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":310,"main":"Drizzle","description":"light intensity drizzle rain","icon":"09n"}],"base":"stations","main":{"temp":283.77,"pressure":1008,"humidity":87,"temp_min":282.15,"temp_max":284.45},"visibility":10000,"wind":{"speed":5.1,"deg":240},"clouds":{"all":75},"dt":1456114362,"sys":{"type":1,"id":5089,"message":0.0435,"country":"GB","sunrise":1456124483,"sunset":1456162053},"id":2643743,"name":"London","cod":200}
London
test is over
.
----------------------------------------------------------------------
Ran 2 tests in 0.209s

OK

C:\Python27>python SeleniumPythonRestApiUnitTest.py
http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=70926ddfd37fdf454548b8db13695995
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":310,"main":"Drizzle","description":"light intensity drizzle rain","icon":"09n"}],"base":"stations","main":{"temp":283.77,"pressure":1008,"humidity":87,"temp_min":282.15,"temp_max":284.45},"visibility":10000,"wind":{"speed":5.1,"deg":240},"clouds":{"all":75},"dt":1456114362,"sys":{"type":1,"id":5089,"message":0.0435,"country":"GB","sunrise":1456124483,"sunset":1456162053},"id":2643743,"name":"London","cod":200}
------- test is over -------
--------------------
.http://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=70926ddfd37fdf454548b8db13695995
{"coord":{"lon":-0.13,"lat":51.51},"weather":[{"id":310,"main":"Drizzle","description":"light intensity drizzle rain","icon":"09n"}],"base":"stations","main":{"temp":283.77,"pressure":1008,"humidity":87,"temp_min":282.15,"temp_max":284.45},"visibility":10000,"wind":{"speed":5.1,"deg":240},"clouds":{"all":75},"dt":1456114362,"sys":{"type":1,"id":5089,"message":0.0435,"country":"GB","sunrise":1456124483,"sunset":1456162053},"id":2643743,"name":"London","cod":200}
city name is:London
------- test is over -------
--------------------
.
----------------------------------------------------------------------
Ran 2 tests in 0.145s

OK

C:\Python27>python SeleniumPythonRestApiUnitTest.py
http://api.openweathermap.org/data/2.5/weather?q=Baltimore,us&APPID=70926ddfd37fdf454548b8db13695995
{"coord":{"lon":-76.61,"lat":39.29},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"cmc stations","main":{"temp":281.958,"pressure":1020.68,"humidity":96,"temp_min":281.958,"temp_max":281.958,"sea_level":1027.3,"grnd_level":1020.68},"wind":{"speed":3.68,"deg":8.00613},"rain":{"3h":0.94},"clouds":{"all":76},"dt":1456113748,"sys":{"message":0.0066,"country":"US","sunrise":1456141772,"sunset":1456181455},"id":4347778,"name":"Baltimore","cod":200}
------- test is over -------
.http://api.openweathermap.org/data/2.5/weather?q=Baltimore,us&APPID=70926ddfd37fdf454548b8db13695995
{"coord":{"lon":-76.61,"lat":39.29},"weather":[{"id":500,"main":"Rain","description":"light rain","icon":"10n"}],"base":"cmc stations","main":{"temp":281.958,"pressure":1020.68,"humidity":96,"temp_min":281.958,"temp_max":281.958,"sea_level":1027.3,"grnd_level":1020.68},"wind":{"speed":3.68,"deg":8.00613},"rain":{"3h":0.94},"clouds":{"all":76},"dt":1456113748,"sys":{"message":0.0066,"country":"US","sunrise":1456141772,"sunset":1456181455},"id":4347778,"name":"Baltimore","cod":200}
city name is:Baltimore
------- test is over -------
.
----------------------------------------------------------------------
Ran 2 tests in 0.166s

OK

C:\Python27>