This class provides a base for functional testing without database access, extending the standard unit testing framework with specialized assertions for web applications. It includes a built-in test client for simulating requests, utilities for validating templates and forms, and tools for comparing HTML, XML, and JSON content. Additionally, it enforces database isolation by preventing unauthorized database queries during test execution.
Attributes
| Attribute | Type | Description |
|---|
| client_class | class = Client | The class we'll use for the test client self.client. |
| async_client_class | class = AsyncClient | The class used to instantiate the asynchronous test client available as self.async_client. |
| databases | set or string = set() | A set of database aliases that the test is allowed to access, or the string "all" to allow all. |
Constructor
Signature
def SimpleTestCase(
methodName: str = "runTest"
)
Parameters
| Name | Type | Description |
|---|
| methodName | str = "runTest" | The name of the specific test method to be executed. |
Methods
setUpClass()
@classmethod
def setUpClass()
Initializes the test class by applying overridden settings, configuring database failure mocks, and registering cleanup tasks.
ensure_connection_patch_method()
@classmethod
def ensure_connection_patch_method() - > function
Returns a patched version of the database connection method that enforces database access restrictions for the test case.
Returns
| Type | Description |
|---|
function | A wrapper function that checks database permissions before allowing a connection |
debug()
Perform the same as call(), without catching the exception.
settings()
@classmethod
def settings(
**kwargs: any
) - > contextmanager
A context manager that temporarily sets a setting and reverts to the original value when exiting the context.
Parameters
| Name | Type | Description |
|---|
| **kwargs | any | Setting names and their temporary values |
Returns
| Type | Description |
|---|
contextmanager | A context manager for overriding Django settings |
modify_settings()
@classmethod
def modify_settings(
**kwargs: any
) - > contextmanager
A context manager that temporarily applies changes to a list setting and reverts back to the original value when exiting the context.
Parameters
| Name | Type | Description |
|---|
| **kwargs | any | Setting names and the modifications to apply |
Returns
| Type | Description |
|---|
contextmanager | A context manager for appending, prepending, or removing items from list-based settings |
assertRedirects()
@classmethod
def assertRedirects(
response: [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse),
expected_url: string,
status_code: integer,
target_status_code: integer,
msg_prefix: string,
fetch_redirect_response: boolean
)
Assert that a response redirected to a specific URL and that the redirect URL can be loaded.
Parameters
| Name | Type | Description |
|---|
| response | [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The response object to check for redirection |
| expected_url | string | The URL path the response is expected to redirect to |
| status_code | integer | The expected HTTP status code of the redirect response (default 302) |
| target_status_code | integer | The expected HTTP status code of the final destination URL (default 200) |
| msg_prefix | string | A prefix to prepend to the failure message |
| fetch_redirect_response | boolean | Whether to use the test client to fetch the destination URL and verify its status code |
assertURLEqual()
@classmethod
def assertURLEqual(
url1: string,
url2: string,
msg_prefix: string
)
Assert that two URLs are the same, ignoring the order of query string parameters except for parameters with the same name.
Parameters
| Name | Type | Description |
|---|
| url1 | string | The first URL to compare |
| url2 | string | The second URL to compare |
| msg_prefix | string | A prefix to prepend to the failure message |
assertContains()
@classmethod
def assertContains(
response: [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse),
text: string|bytes,
count: integer|null,
status_code: integer,
msg_prefix: string,
html: boolean
)
Assert that a response indicates that some content was retrieved successfully, (i.e., the HTTP status code was as expected) and that text occurs count times in the content of the response.
Parameters
| Name | Type | Description |
|---|
| response | [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The response to check |
| text | `string | bytes` |
| count | `integer | null` |
| status_code | integer | The expected HTTP status code (default 200) |
| msg_prefix | string | Prefix for the failure message |
| html | boolean | If true, performs a semantic HTML comparison instead of a literal string search |
assertNotContains()
@classmethod
def assertNotContains(
response: [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse),
text: string|bytes,
status_code: integer,
msg_prefix: string,
html: boolean
)
Assert that a response indicates that some content was retrieved successfully, (i.e., the HTTP status code was as expected) and that text doesn't occur in the content of the response.
Parameters
| Name | Type | Description |
|---|
| response | [HttpResponse](../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The response to check |
| text | `string | bytes` |
| status_code | integer | The expected HTTP status code (default 200) |
| msg_prefix | string | Prefix for the failure message |
| html | boolean | If true, performs a semantic HTML comparison |
@classmethod
def assertFormError(
form: [Form](../../forms/forms/form.md?sid=django_forms_forms_form),
field: string|null,
errors: string|list,
msg_prefix: string
)
Assert that a field named "field" on the given form object has specific errors.
Parameters
| Name | Type | Description |
|---|
| form | [Form](../../forms/forms/form.md?sid=django_forms_forms_form) | The form instance to validate |
| field | `string | null` |
| errors | `string | list` |
| msg_prefix | string | Prefix for the failure message |
@classmethod
def assertFormSetError(
formset: [BaseFormSet](../../forms/formsets/baseformset.md?sid=django_forms_formsets_baseformset),
form_index: integer|null,
field: string|null,
errors: string|list,
msg_prefix: string
)
Similar to assertFormError() but for formsets.
Parameters
| Name | Type | Description |
|---|
| formset | [BaseFormSet](../../forms/formsets/baseformset.md?sid=django_forms_formsets_baseformset) | The formset instance to validate |
| form_index | `integer | null` |
| field | `string | null` |
| errors | `string | list` |
| msg_prefix | string | Prefix for the failure message |
assertTemplateUsed()
@classmethod
def assertTemplateUsed(
response: HttpResponse|null,
template_name: string|null,
msg_prefix: string,
count: integer|null
) - > contextmanager|null
Assert that the template with the provided name was used in rendering the response. Also usable as context manager.
Parameters
| Name | Type | Description |
|---|
| response | `HttpResponse | null` |
| template_name | `string | null` |
| msg_prefix | string | Prefix for the failure message |
| count | `integer | null` |
Returns
| Type | Description |
|---|
| `contextmanager | null` |
assertTemplateNotUsed()
@classmethod
def assertTemplateNotUsed(
response: HttpResponse|null,
template_name: string|null,
msg_prefix: string
) - > contextmanager|null
Assert that the template with the provided name was NOT used in rendering the response. Also usable as context manager.
Parameters
| Name | Type | Description |
|---|
| response | `HttpResponse | null` |
| template_name | `string | null` |
| msg_prefix | string | Prefix for the failure message |
Returns
| Type | Description |
|---|
| `contextmanager | null` |
assertRaisesMessage()
@classmethod
def assertRaisesMessage(
expected_exception: type,
expected_message: string,
*args: any,
**kwargs: any
) - > contextmanager|null
Assert that expected_message is found in the message of a raised exception.
Parameters
| Name | Type | Description |
|---|
| expected_exception | type | Exception class expected to be raised. |
| expected_message | string | expected error message string value. |
| *args | any | Function to be called and extra positional args. |
| **kwargs | any | Extra kwargs. |
Returns
| Type | Description |
|---|
| `contextmanager | null` |
assertWarnsMessage()
@classmethod
def assertWarnsMessage(
expected_warning: type,
expected_message: string,
*args: any,
**kwargs: any
) - > contextmanager|null
Same as assertRaisesMessage but for assertWarns() instead of assertRaises().
Parameters
| Name | Type | Description |
|---|
| expected_warning | type | The warning class expected to be triggered |
| expected_message | string | The expected warning message string |
| *args | any | Function to be called and extra positional args |
| **kwargs | any | Extra kwargs |
Returns
| Type | Description |
|---|
| `contextmanager | null` |
assertFieldOutput()
@classmethod
def assertFieldOutput(
fieldclass: type,
valid: dict,
invalid: dict,
field_args: list|null,
field_kwargs: dict|null,
empty_value: any
)
Assert that a form field behaves correctly with various inputs.
Parameters
| Name | Type | Description |
|---|
| fieldclass | type | the class of the field to be tested. |
| valid | dict | a dictionary mapping valid inputs to their expected cleaned values. |
| invalid | dict | a dictionary mapping invalid inputs to one or more raised error messages. |
| field_args | `list | null` |
| field_kwargs | `dict | null` |
| empty_value | any | the expected clean output for inputs in empty_values |
assertHTMLEqual()
@classmethod
def assertHTMLEqual(
html1: string,
html2: string,
msg: string|null
)
Assert that two HTML snippets are semantically the same. Whitespace in most cases is ignored, and attribute ordering is not significant. The arguments must be valid HTML.
Parameters
| Name | Type | Description |
|---|
| html1 | string | The first HTML snippet to compare |
| html2 | string | The second HTML snippet to compare |
| msg | `string | null` |
assertHTMLNotEqual()
@classmethod
def assertHTMLNotEqual(
html1: string,
html2: string,
msg: string|null
)
Assert that two HTML snippets are not semantically equivalent.
Parameters
| Name | Type | Description |
|---|
| html1 | string | The first HTML snippet |
| html2 | string | The second HTML snippet |
| msg | `string | null` |
assertInHTML()
@classmethod
def assertInHTML(
needle: string,
haystack: string,
count: integer|null,
msg_prefix: string
)
Asserts that a specific HTML fragment (needle) is present within a larger HTML string (haystack) a specific number of times.
Parameters
| Name | Type | Description |
|---|
| needle | string | The HTML fragment to search for |
| haystack | string | The HTML content to search within |
| count | `integer | null` |
| msg_prefix | string | Prefix for the failure message |
assertNotInHTML()
@classmethod
def assertNotInHTML(
needle: string,
haystack: string,
msg_prefix: string
)
Asserts that a specific HTML fragment is not present within the provided HTML content.
Parameters
| Name | Type | Description |
|---|
| needle | string | The HTML fragment that should be absent |
| haystack | string | The HTML content to search within |
| msg_prefix | string | Prefix for the failure message |
assertJSONEqual()
@classmethod
def assertJSONEqual(
raw: string,
expected_data: string|dict|list,
msg: string|null
)
Assert that the JSON fragments raw and expected_data are equal. Usual JSON non-significant whitespace rules apply as the heavyweight is delegated to the json library.
Parameters
| Name | Type | Description |
|---|
| raw | string | The raw JSON string to validate |
| expected_data | `string | dict |
| msg | `string | null` |
assertJSONNotEqual()
@classmethod
def assertJSONNotEqual(
raw: string,
expected_data: string|dict|list,
msg: string|null
)
Assert that the JSON fragments raw and expected_data are not equal. Usual JSON non-significant whitespace rules apply as the heavyweight is delegated to the json library.
Parameters
| Name | Type | Description |
|---|
| raw | string | The raw JSON string to validate |
| expected_data | `string | dict |
| msg | `string | null` |
assertXMLEqual()
@classmethod
def assertXMLEqual(
xml1: string,
xml2: string,
msg: string|null
)
Assert that two XML snippets are semantically the same. Whitespace in most cases is ignored and attribute ordering is not significant. The arguments must be valid XML.
Parameters
| Name | Type | Description |
|---|
| xml1 | string | The first XML snippet to compare |
| xml2 | string | The second XML snippet to compare |
| msg | `string | null` |
assertXMLNotEqual()
@classmethod
def assertXMLNotEqual(
xml1: string,
xml2: string,
msg: string|null
)
Assert that two XML snippets are not semantically equivalent. Whitespace in most cases is ignored and attribute ordering is not significant. The arguments must be valid XML.
Parameters
| Name | Type | Description |
|---|
| xml1 | string | The first XML snippet |
| xml2 | string | The second XML snippet |
| msg | `string | null` |