Test

The test module offers extra functions for unit tests.

class django_marina.test.clients.ExtendedClient(enforce_csrf_checks=False, raise_request_exception=True, *, headers=None, **defaults)[source]

Client with added features.

force_login(user, backend=None)[source]

Force given user to login.

When user is ExtendedClient.USER_IGNORE, this method does nothing. When user is None, this will force a logout. In all other cases, user will be logged in.

generic(*args, user=-1, **kwargs)[source]

Force given user to login, then fetch request and return response.

This adds the keyword argument user to all methods that end up calling generic, including get() and post().

When no user is set to ExtendedClient.USER_IGNORE (default), no special action is taken. When user is None, the request will be performed on an anonymous request (user logged out). When user is provided and not None, the request will be performed with that user logged in.

request(**request)[source]

Request a response from the server.

class django_marina.test.test_cases.ExtendedTestCase(methodName='runTest')[source]

TestCase with a extended client and extra features for asserting response content.

assertAllowed(path, user, **kwargs)[source]

Make request, assert that response has status code OK.

If user contains a list of users, the assertion will be made for every user in that list.

Parameters:
  • path – Path for request

  • user – User or list of users for request

  • kwargs – Kwargs for request

assertContainsSelector(response, selector, count=None, status_code=200, msg_prefix='', **kwargs)[source]

Assert that CSS selector can be found in response.

Documentation on CSS selectors: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#css-selectors

To search within the found selectors, you can optionally provide a string argument. This will filter the found tags. See https://www.crummy.com/software/BeautifulSoup/bs4/doc/#the-string-argument

assertContainsTag(response, name=None, count=None, status_code=200, msg_prefix='', **kwargs)[source]

Assert that tag can be found in response, using BeautifulSoup find notation.

Documentation on tag finding: https://www.crummy.com/software/BeautifulSoup/bs4/doc/#kinds-of-filters

assertForbidden(path, user, **kwargs)[source]

Make request, assert that response has status code Forbidden.

If user contains a list of users, the assertion will be made for every user in that list.

Parameters:
  • path – Path for request

  • user – User or list of users for request

  • kwargs – Kwargs for request

assertHTMLEqual(html1, html2, msg=None, ignore_attrs=None)[source]

Assert that the strings html1 and html2 are equal, except for certain attributes to ignore.

The comparison is based on HTML semantics.

assertHasMessage(response, message)[source]

Assert that response has given message.

Parameters:
  • response – Response object

  • message – Full text of message to check for

assertLoginNotRequired(path, **kwargs)[source]

Make request while not logged in, assert that response has status code OK.

Parameters:
  • path – Path for request

  • kwargs – Kwargs for request

assertLoginRequired(path, **kwargs)[source]

Make request while not logged in, assert that response has status code Forbidden or redirects to login page.

Parameters:
  • path – Path for request

  • kwargs – Kwargs for request

assertNotContainsSelector(response, selector, status_code=200, msg_prefix='', **kwargs)[source]

Assert that CSS selector cannot be found in response.

assertNotContainsTag(response, name=None, status_code=200, msg_prefix='', **kwargs)[source]

Assert that tag cannot be found in response, using BeautifulSoup find notation.

See assertContainsTag for documentation.

assertNotFound(path, user, **kwargs)[source]

Make request, assert that response has status code Not Found.

If user contains a list of users, the assertion will be made for every user in that list.

Parameters:
  • path – Path for request

  • user – User or list of users for request

  • kwargs – Kwargs for request

assertResponseOk(response)[source]

Assert that response has status code OK.

Parameters:

response – HttpResponse

assertResponseStatusCode(response, status_code, msg_prefix=None)[source]

Assert that response has given status code.

Parameters:
  • response – HttpResponse

  • status_code – int

  • msg_prefix – str

client_class

alias of ExtendedClient