Writing the first round of unit tests

Now, we will write the first round of unit tests. Specifically, we will write unit tests related to the LED resources. Test fixtures provide a fixed baseline to enable us to reliably and repeatedly execute tests. Pytest makes it easy to declare a test fixture function by marking a function with the @pytest.fixture decorator. Then, whenever we use the fixture function name as an argument in a test function declaration, pytest will make the fixture function provide the fixture object.

The pytest-tornasync plugin provides us with many fixtures that we will use to easily write tests for our Tornado API. In order to work with this plugin, we must declare a fixture function, named app, that returns a tornado.web.Application instance. In our case, this fixture function will return an instance of the Application class, which maps the URL patterns to asynchronous and non-blocking request handlers. We don't need to specify an app as an argument for the test functions, because the pytest-tornadoasync plugin will use the app fixture under the hood in other fixtures, such as the http_server_client fixture, which we will use to create an asynchronous HTTP client to compose and send HTTP requests in our tests.

Create a new tests.py file within the virtual environment's root folder (Tornado01). Add the following lines, which declare many import statements, the app fixture, and the test_set_and_get_leds_brightness_levels test function. The code file for the sample is included in the restful_python_2_11_01 folder, in the Tornado01/tests.py file:

from async_drone_service import Application 
from http import HTTPStatus 
import json 
import pytest 
from tornado import escape 
from tornado.httpclient import HTTPClientError 
 
 
@pytest.fixture 
def app(): 
    application = Application(debug=False) 
    return application 
 
 
async def 
test_set_and_get_leds_brightness_levels(http_server_client): """ Ensure we can set and get the brightness levels for the three LEDs """ for led_id in range(1, 4): patch_args = {'brightness_level': led_id * 60} patch_response = await http_server_client.fetch( '/leds/{}'.format(led_id), method='PATCH', body=json.dumps(patch_args)) assert patch_response.code == HTTPStatus.OK get_response = await http_server_client.fetch( '/leds/{}'.format(led_id), method='GET') assert get_response.code == HTTPStatus.OK get_response_data = escape.json_decode(get_response.body) assert 'brightness_level' in get_response_data.keys() assert get_response_data['brightness_level'] == patch_args['brightness_level']

First, the code declares the app fixture function, which creates an instance of the Application class, defined in the async_drone_service module with the debug argument set to False, and returns the instance.

The test_set_and_get_leds_brightness_levels asynchronous method tests whether we can set and get the brightness levels for the red, green, and blue LEDs. The code composes and sends three HTTP PATCH methods to set new brightness level values for the LEDs whose IDs are equal to 1, 2, and 3. The code sets a different brightness level for each LED. Notice that the method is declared with the async keyword before def to indicate that the method is an asynchronous method, that is, a coroutine.

The method uses the test client received in the http_server_client argument, which provides an asynchronous HTTP client for tests and calls its fetch method to compose and send the HTTP PATCH requests. http_server_client fetches the app fixture that we defined.

Notice that the call to this method uses the await keyword to obtain the result of the coroutine execution. The code calls json.dumps with the dictionary to be sent as the body of an argument for the fetch method.

Then, the code uses the http_server_client.fetch method again with the await keyword to compose and send 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 assert to check for the following expected results for each LED:

  • The status_code for the HTTP PATCH response is HTTP 200 OK (HTTPStatus.OK)
  • The status_code for the HTTP GET response is HTTP 200 OK (HTTPStatus.OK)
  • The response body for the HTTP GET response includes a key named brigthness_level
  • The value for the brightness_level key in the HTTP GET response matches the brightness level set to the LED
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.80