The on_commit function has been added to Django 1.9 to be able to trigger an action (e.g. a Celery task) after the current transaction has committed.
They mention later in the docs that one should use TransactionTestCase
to test features that rely on that function. However, unlike TestCase
(which uses transactions and rolls them back), TransactionTestCase
empties the whole database after each test.
Unfortunately, I have data migrations that preload some useful data inside the database, which means that subsequent tests do not work anymore after the first test clears the database.
I ended up resorting to a dirty trick by mocking on_commit
:
with mock.patch.object(django.db.transaction, 'on_commit', lambda t: t()):
test_something()
Is there a better way?
Aucun commentaire:
Enregistrer un commentaire