Now, we will write a first round of unit tests. Specifically, we will write unit tests related to the LED resources. Create a new tests
subfolder within the virtual environment's root folder. Then, create a new test_hexacopter.py
file within the new tests
subfolder. Add the following lines that declare many import
statements and the TextHexacopter
class. The code file for the sample is included in the restful_python_chapter_10_02
folder:
import unittest import status import json from tornado import ioloop, escape from tornado.testing import AsyncHTTPTestCase, gen_test, gen from async_api import Application class TestHexacopter(AsyncHTTPTestCase): def get_app(self): self.app = Application(debug=False) return self.app def test_set_and_get_led_brightness_level(self): """ Ensure we can set and get the brightness levels for both LEDs """ patch_args_led_1 = {'brightness_level': 128} patch_args_led_2 = {'brightness_level': 250} patch_response_led_1 = self.fetch( '/leds/1', method='PATCH', body=json.dumps(patch_args_led_1)) patch_response_led_2 = self.fetch( '/leds/2', method='PATCH', body=json.dumps(patch_args_led_2)) self.assertEqual(patch_response_led_1.code, status.HTTP_200_OK) self.assertEqual(patch_response_led_2.code, status.HTTP_200_OK) get_response_led_1 = self.fetch( '/leds/1', method='GET') get_response_led_2 = self.fetch( '/leds/2', method='GET') self.assertEqual(get_response_led_1.code, status.HTTP_200_OK) self.assertEqual(get_response_led_2.code, status.HTTP_200_OK) get_response_led_1_data = escape.json_decode(get_response_led_1.body) get_response_led_2_data = escape.json_decode(get_response_led_2.body) self.assertTrue('brightness_level' in get_response_led_1_data.keys()) self.assertTrue('brightness_level' in get_response_led_2_data.keys()) self.assertEqual(get_response_led_1_data['brightness_level'], patch_args_led_1['brightness_level']) self.assertEqual(get_response_led_2_data['brightness_level'], patch_args_led_2['brightness_level'])
The TestHexacopter
class is a subclass of tornado.testing.AsyncHTTPTestCase
, that is, a test case that starts up a Tornado HTTP Server. The class overrides the get_app
method that returns the tornado.web.Application
instance that we want to test. In this case, we return an instance of the Application
class declared in the async_api
module, with the debug
argument set to False
.
The test_set_and_get_led_brightness_level
method tests whether we can set and get the brightness levels for both the white and blue LED. The code composes and sends two HTTP PATCH
methods to set new brightness level values for the LEDs whose IDs are equal to 1
and 2
. The code sets a different brightness level for each LED.
The code calls the self.fetch method to compose and send the HTTP PATCH
request and calls json.dumps
with the dictionary to be sent to the body as an argument. Then, the code uses self.fetch
again to compose and send two HTTP GET
methods to retrieve the brightness level values for the LEDs whose brightness values have been modified. The code uses tornado.escape.json_decode
to convert the bytes in the response body to a Python dictionary. The method uses assertEqual
and assertTrue
to check for the following expected results:
status_code
for the two HTTP PATCH
responses is HTTP 200 OK (status.HTTP_200_OK
)status_code
for the two HTTP GET
responses is HTTP 200 OK (status.HTTP_200_OK
)GET
responses include a key named brigthness_level
brightness_level
key in the HTTP GET
responses are equal to the brightness level set to each LED3.148.103.210