TransactionTestCase
This class provides a base for test cases that require database interaction without wrapping tests in a transaction. It supports features such as automatic fixture loading, database flushing between tests, and the ability to reset auto-increment sequences or restrict the app registry for faster execution. It is particularly useful for testing code that performs its own transaction management or requires a clean database state across multiple connections.
Attributes
| Attribute | Type | Description |
|---|---|---|
| reset_sequences | boolean = False | Subclasses can ask for resetting of auto increment sequence before each test case |
| available_apps | iterable of strings = null | Subclasses can enable only a subset of apps for faster tests |
| fixtures | list of strings = null | Subclasses can define fixtures which will be automatically installed. |
| databases | set of strings = {DEFAULT_DB_ALIAS} | A set of database aliases that the test case is allowed to access for proper test isolation. |
| serialized_rollback | boolean = False | This flag allows enabling on a per-case basis the serialization of database contents into a fixture during setup to restore data during teardown. |
Constructor
Signature
def TransactionTestCase()
Methods
setUpClass()
@classmethod
def setUpClass()
Initializes the test class and triggers pre-test setup if the class is not a subclass of TestCase.
tearDownClass()
@classmethod
def tearDownClass()
Cleans up class-level resources and restores the application registry if available_apps was restricted.
assertQuerySetEqual()
@classmethod
def assertQuerySetEqual(
qs: [QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset),
values: iterable,
transform: callable = None,
ordered: bool = True,
msg: str = None
) - > bool
Asserts that a QuerySet matches a specific list of values after an optional transformation.
Parameters
| Name | Type | Description |
|---|---|---|
| qs | [QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset) | The QuerySet or iterable to be validated |
| values | iterable | The expected sequence of values to compare against the QuerySet |
| transform | callable = None | A function applied to every item in the QuerySet before comparison |
| ordered | bool = True | Whether the order of elements must match; if False, a frequency-based comparison is used |
| msg | str = None | The custom error message to display if the assertion fails |
Returns
| Type | Description |
|---|---|
bool | The result of the equality assertion |
assertNumQueries()
@classmethod
def assertNumQueries(
num: int,
func: callable = None,
using: str = DEFAULT_DB_ALIAS
) - > object
Asserts that the number of database queries executed matches the expected count.
Parameters
| Name | Type | Description |
|---|---|---|
| num | int | The exact number of database queries expected |
| func | callable = None | The function to execute and monitor for database queries |
| using | str = DEFAULT_DB_ALIAS | The database alias to monitor for query execution |
Returns
| Type | Description |
|---|---|
object | A context manager if func is None, otherwise the result of the function call |