Getting ready for unit testing with pytest

So far, we have been writing code to add features to our RESTful Web Service and configuring many settings for the Django REST framework. We used command-line and GUI tools to understand how all the pieces worked together and to check the results of diverse HTTP requests. Now, we will write unit tests that will allow us to make sure that our RESTful Web Service will work as expected. Before we can start writing unit tests, it is necessary to install many additional packages in our virtual environment. Make sure you quit Django's development server. Remember that you just need to press Ctrl + C in the terminal or go to the Command Prompt window in which it is running. First, we will make some changes to work with a single version of our RESTful Web Service.

This way, it will be easier to focus on tests for a single version in our examples. Replace the code in the urls.py file in the restful01/restful01 folder, specifically, the restful01/restful01/urls.py file. The file defines the root URL configurations, and therefore, we want to include only the URL patterns for the first version of our web service. The code file for the sample is included in the hillar_django_restful_10_01 folder, in the restful01/restful01/urls.py file:

from django.conf.urls import url, include 
 
urlpatterns = [ 
    url(r'^', include('drones.urls')), 
    url(r'^api-auth/', include('rest_framework.urls')) 
]

We will install the following Python packages in our virtual environment:

  • pytest: This is a very popular Python unit test framework that makes testing easy and reduces boilerplate code
  • pytest-django: This pytest plugin allows us to easily use and configure the features provided by pytest in our Django tests
Notice that we won't be working with Django's manage.pytest command. We will work with pytest instead. However, in case you don't want to work with pytest, most of the things you will learn can be easily adapted to any other test framework. In fact, the code is compatible with nose in case you decide to use the most common, yet a bit outdated, configuration for testing with the Django REST framework. Nowadays, pytest is the preferred unit test framework for Python.

Run the following command to install the pytest package:

pip install pytest

The last lines for the output will indicate that the pytest package and its required packages have been successfully installed:

Installing collected packages: attrs, pluggy, six, py, pytest  Running setup.py install for pluggy ... doneSuccessfully installed attrs-17.3.0 pluggy-0.6.0 py-1.5.2 pytest-3.3.1 six-1.11.0

We just need to run the following command to install the pytest-django package:

pip install pytest-django

The last lines for the output will indicate that the pytest-django package has been successfully installed:

Installing collected packages: pytest-django
Successfully installed pytest-django-3.1.2

Now, go to the restful01 folder that contains the manage.py file and create a new file named pytest.ini. Write the following code in this new file. The following lines show the code for this file that specifies the Django settings module (restful01.settings) and the pattern that pytest will use to locate the Python files, the declare tests. The code file for the sample is included in the hillar_django_restful_10_01 folder in the restful01/pytest.ini file:

[pytest] 
DJANGO_SETTINGS_MODULE = restful01.settings 
python_files = tests.py test_*.py *_tests.py 

Whenever we execute pytest to run tests, the test runner will check the following to find test definitions:

  • Python files named tests.py
  • Python files whose name starts with the test_ prefix
  • Python files whose name ends with the _tests suffix

In Chapter 9Applying Throttling Rules and Versioning Management, we configured throttling rules for our RESTful Web Service. We want to run our tests considering the throttling rules. In fact, we should write tests to make sure that the throttling rules are working OK. We will be running requests many times, and therefore, the low values we used for the throttling rules might complicate running all the requests required by our tests. Hence, we will increase the values for the throttling rules to simplify our testing samples. Open the restful01/restful01/settings.py file that declares module-level variables that define the configuration of Django for the restful01 project. We will make some changes to this Django settings file. Replace the code for the highlighted lines included in the REST_FRAMEWORK dictionary. The following lines show the new declaration of the REST_FRAMEWORK dictionary. The code file for the sample is included in the hillar_django_restful_10_01 folder in the restful01/restful01/settings.py file:

REST_FRAMEWORK = { 
    'DEFAULT_PAGINATION_CLASS': 
    'drones.custompagination.LimitOffsetPaginationWithUpperBound', 
    'PAGE_SIZE': 4, 
    'DEFAULT_FILTER_BACKENDS': ( 
        'django_filters.rest_framework.DjangoFilterBackend', 
        'rest_framework.filters.OrderingFilter', 
        'rest_framework.filters.SearchFilter', 
        ), 
    'DEFAULT_AUTHENTICATION_CLASSES': ( 
        'rest_framework.authentication.BasicAuthentication', 
        'rest_framework.authentication.SessionAuthentication', 
        ), 
    'DEFAULT_THROTTLE_CLASSES': ( 
        'rest_framework.throttling.AnonRateThrottle', 
        'rest_framework.throttling.UserRateThrottle', 
    ), 
    'DEFAULT_THROTTLE_RATES': { 
        'anon': '300/hour', 
        'user': '100/hour', 
        'drones': '200/hour', 
        'pilots': '150/hour', 
    } 
} 

We increased the number of requests per hour that we can execute in each of the throttling rates configurations. This way, we will be able to run our tests without issues.

In this case, we are using the same settings file for our tests in order to avoid running additional steps and repeating test settings. However, in most cases, we would create a specific Django configuration file for testing.
..................Content has been hidden....................

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