Reference

class rest_assured.testcases.BaseRESTAPITestCase(methodName='runTest')[source]

Base test case class for testing REST API endpoints.

base_name = None

required: Base route name of the API endpoints to test.

factory_class = None

required: The factory class to use for creating the main object to test against.

LIST_SUFFIX = '-list'

Suffix for list endpoint view names. Defaults to '-list'.

DETAIL_SUFFIX = '-detail'

Suffix for detail endpoint view names. Defaults to '-detail'.

lookup_field = 'pk'

The field to use for DB and route lookups. Defaults to 'pk'.

user_factory = None

User factory to use in case you need user authentication for testing. Defaults to None.

object = None

The main test subject.

user = None

The user instance created if the user_factory is set and used. Defaults to None.

get_factory_class()[source]

Return the factory class for generating the main object (or model instance) of this test case.

By default this gets the factory_class attribute of this class.

Returns:Factory class used for creating the mock objects.
get_object(factory)[source]

Create and return the object (or model instance) of this test case.

By default this calls the create() method of the factory class, assuming a Django Model or a factory_boy’s Factory.

Parameters:factory – The factory class used for creating
Returns:The main object of this test case.
setUp()[source]

Generates the main object and user instance if needed.

The user instance will be created only if the user_factory attribute is set to the factory class.

If there is an available user instance, that user will be force authenticated.

class rest_assured.testcases.ListAPITestCaseMixin[source]

Adds a list view test to the test case.

pagination_results_field = None

When using pagination set this attribute to the name of the property in the response data that holds the result set. Defaults to None.

get_list_url()[source]

Return the list endpoint url.

Returns:The url of list endpoint.
get_list_response(**kwargs)[source]

Send the list request and return the response.

Parameters:kwargs – Extra arguments that are passed to the client’s get() call.
Returns:The response object.
test_list(**kwargs)[source]

Send request to the list view endpoint, verify and return the response.

Checks for a 200 status code and that there is a results property in the response.data.

You can extend it for more extensive checks.

example

class LanguageRESTAPITestCase(ListAPITestCaseMixin, BaseRESTAPITestCase):

    def test_list(self, **kwargs):
        response = super(LanguageRESTAPITestCase, self).test_list(**kwargs)
        results = response.data.get('results')
        self.assertEqual(results[0].get('code'), self.object.code)
Parameters:kwargs – Extra arguments that are passed to the client’s get() call.
Returns:The view’s response.
class rest_assured.testcases.DetailAPITestCaseMixin[source]

Adds a detail view test to the test case.

get_detail_url()[source]

Return the detail endpoint url.

Returns:The url of detail endpoint.
get_detail_response(**kwargs)[source]

Send the detail request and return the response.

Parameters:kwargs – Extra arguments that are passed to the client’s get() call.
Returns:The response object.
test_detail(**kwargs)[source]

Send request to the detail view endpoint, verify and return the response.

Checks for a 200 status code and that there is an id property in the response.data and that it equals the main object’s id.

You can extend it for more extensive checks.

example

class LanguageRESTAPITestCase(DetailAPITestCaseMixin, BaseRESTAPITestCase):

    def test_list(self, **kwargs):
        response = super(LanguageRESTAPITestCase, self).test_list(**kwargs)
        self.assertEqual(response.data.get('code'), self.object.code)

Using a callable in attributes_to_check:

example

class TaggedFoodRESTAPITestCase(DetailAPITestCaseMixin, BaseRESTAPITestCase):

    attributes_to_check = ['name', ('similar', lambda obj: obj.tags.similar_objects())]
Parameters:kwargs – Extra arguments that are passed to the client’s get() call.
Returns:The view’s response.
class rest_assured.testcases.CreateAPITestCaseMixin[source]

Adds a create view test to the test case.

create_data = None

required: Dictionary of data to use as the POST request’s body.

response_lookup_field = 'id'

The name of the field in the response data for looking up the created object in DB.

get_create_data()[source]

Return the data used for the create request.

By default gets the create_data attribute of this class.

Returns:The data dictionary.
get_create_url()[source]

Return the create endpoint url.

Returns:The url of create endpoint.
get_create_response(data=None, **kwargs)[source]

Send the create request and return the response.

Parameters:
  • data – A dictionary of the data to use for the create request.
  • kwargs – Extra arguments that are passed to the client’s post() call.
Returns:

The response object.

get_lookup_from_response(data)[source]

Return value for looking up the created object in DB.

Note:The created object will be looked up using the lookup_field attribute as key, which defaults to pk.
Parameters:data – A dictionary of the response data to lookup the field in.
Returns:The value for looking up the
test_create(data=None, **kwargs)[source]

Send request to the create view endpoint, verify and return the response.

Also verifies that the object actually exists in the database.

Parameters:
  • data – A dictionary of the data to use for the create request.
  • kwargs – Extra arguments that are passed to the client’s post() call.
Returns:

A tuple response, created of the view’s response the created instance.

class rest_assured.testcases.DestroyAPITestCaseMixin[source]

Adds a destroy view test to the test case.

get_destroy_url()[source]

Return the destroy endpoint url.

Returns:The url of destroy endpoint.
get_destroy_response(**kwargs)[source]

Send the destroy request and return the response.

Parameters:kwargs – Extra arguments that are passed to the client’s delete() call.
Returns:The view’s response.
test_destroy(**kwargs)[source]

Send request to the destroy view endpoint, verify and return the response.

Also verifies the object does not exist anymore in the database.

Parameters:kwargs – Extra arguments that are passed to the client’s delete() call.
Returns:The view’s response.
class rest_assured.testcases.UpdateAPITestCaseMixin[source]

Adds an update view test to the test case.

use_patch = True

Whether to send a PATCH request instead of PUT. Defaults to True.

update_data = None

required: Dictionary of data to use as the update request’s body.

update_results = None

Dictionary mapping attributes to values to check against the updated instance in the database. Defaults to update_data.

relationship_lookup_field = 'id'

The name of the field in the response data for looking up the created object in DB.

get_update_url()[source]

Return the update endpoint url.

Returns:The url of update endpoint.
get_update_response(data=None, results=None, use_patch=None, **kwargs)[source]

Send the update request and return the response.

Parameters:
  • data – Data dictionary for the update request.
  • results – Dictionary mapping instance properties to expected values.
  • kwargs – Extra arguments that are passed to the client’s put() or patch() call.
Returns:

The response object.

get_update_data()[source]

Return the data used for the update request.

By default gets the update_data attribute of this class.

Returns:Data dictionary for the update request.
get_update_results(data=None)[source]

Return a dictionary of the expected results of the instance.

By default gets the update_results attribute of this class. If that isn’t set defaults to the data.

Parameters:data – The update request’s data dictionary.
Returns:Dictionary mapping instance properties to expected values.
get_relationship_value(related_obj, key)[source]

Return a value representing a relation to a related model instance.

By default gets the relationship_lookup_field attribute of this class which defaults to id, and converts it to a string.

Parameters:
  • related_obj – The related model instance to convert to a value.
  • key – A string representing the name of the relation, or the key on the updated object.
Returns:

Value representing the relation to assert against.

test_update(data=None, results=None, use_patch=None, **kwargs)[source]

Send request to the update view endpoint, verify and return the response.

Parameters:
  • data – Data dictionary for the update request.
  • results – Dictionary mapping instance properties to expected values.
  • kwargs – Extra arguments that are passed to the client’s put() or patch() call.
Returns:

A tuple response, updated of the view’s response the updated instance.

class rest_assured.testcases.ReadRESTAPITestCaseMixin[source]

Adds the read CRUD operations tests to the test case.

Includes: ListAPITestCaseMixin, DetailAPITestCaseMixin.

class rest_assured.testcases.WriteRESTAPITestCaseMixin[source]

Adds the write CRUD operations tests to the test case.

Includes: CreateAPITestCaseMixin, UpdateAPITestCaseMixin, DestroyAPITestCaseMixin.

class rest_assured.testcases.ReadWriteRESTAPITestCaseMixin[source]

A complete API test case that covers all successful CRUD operation requests.

Includes: ReadRESTAPITestCaseMixin, WriteRESTAPITestCaseMixin.

class rest_assured.contrib.drf_fsm_transitions.TransitionAPITestCaseMixin[source]

Adds the transition() method for testing state transition API endpoints.

This is a handy extension for quickly test-covering API endpoints that are generated using the DRF-FSM-Transition library.

transition(result, route, attribute='status', from_state=None, data=None)[source]

Send request to a transition view endpoint, verify and return the response.

Parameters:
  • result – The expected value of the instance’s attribute.
  • route – The addition to the route, usually the name of the transition action’s name.
  • attribute – Name of the instance’s attribute that holds the state.
  • from_state – A state to update the object to, to initialize the “from” state.
Returns:

The view’s response.