Skip to main content

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

AttributeTypeDescription
reset_sequencesboolean = FalseSubclasses can ask for resetting of auto increment sequence before each test case
available_appsiterable of strings = nullSubclasses can enable only a subset of apps for faster tests
fixtureslist of strings = nullSubclasses can define fixtures which will be automatically installed.
databasesset of strings = {DEFAULT_DB_ALIAS}A set of database aliases that the test case is allowed to access for proper test isolation.
serialized_rollbackboolean = FalseThis 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

NameTypeDescription
qs[QuerySet](../../db/models/query/queryset.md?sid=django_db_models_query_queryset)The QuerySet or iterable to be validated
valuesiterableThe expected sequence of values to compare against the QuerySet
transformcallable = NoneA function applied to every item in the QuerySet before comparison
orderedbool = TrueWhether the order of elements must match; if False, a frequency-based comparison is used
msgstr = NoneThe custom error message to display if the assertion fails

Returns

TypeDescription
boolThe 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

NameTypeDescription
numintThe exact number of database queries expected
funccallable = NoneThe function to execute and monitor for database queries
usingstr = DEFAULT_DB_ALIASThe database alias to monitor for query execution

Returns

TypeDescription
objectA context manager if func is None, otherwise the result of the function call