2👍
Subclass your tests from TransactionTestCase
instead of TestCase
, and then the data will be saved/committed as expected, so you can then examine the data in the database directly.
Per the docs:
Django’s
TestCase
class is a more commonly used subclass of
TransactionTestCase
that makes use of database transaction
facilities to speed up the process of resetting the database to a
known state at the beginning of each test. A consequence of this,
however, is that some database behaviors cannot be tested within a
DjangoTestCase
class. For instance, you cannot test that a block of
code is executing within a transaction, as is required when using
select_for_update()
. In those cases, you should use
TransactionTestCase
.
TransactionTestCase
andTestCase
are identical except for the manner
in which the database is reset to a known state and the ability for
test code to test the effects of commit and rollback:
- A
TransactionTestCase
resets the database after the test runs by
truncating all tables. ATransactionTestCase
may call commit and
rollback and observe the effects of these calls on the database.- A
TestCase
, on the other hand, does not truncate tables after a test.
Instead, it encloses the test code in a database transaction that is
rolled back at the end of the test. This guarantees that the rollback
at the end of the test restores the database to its initial state.