11π
is it possible to force ./manage.py test to flush the database between each test case?
Have a look on the implementation of the command of django.core.management.commands.flush.py.
You can call the flush command from inside your test call (maybe in TestCase.setUp):
management.call_command('flush')
maybe there is a good reason. Does anyone know?
Yes there is: Speed up. Flushing and reloading many data from json takes a whileβ¦
Maybe you should have a look on TransactionTestCase
9π
The answer to this is, donβt write your tests in such a way as they depend on particular key values. For example, your test could better be written:
def test_one(self):
do_something_which_creates_two_log_entries()
logs = LogEntry.objects.all()
assert_log_entry_correct(log[0])
assert_log_entry_correct(log[1])
2π
You can also use TransactionTestCase.reset_sequences:
Setting reset_sequences = True on a TransactionTestCase will make sure sequences are always reset before the test run:
class TestsThatDependsOnPrimaryKeySequences(TransactionTestCase): reset_sequences = True def test_animal_pk(self): lion = Animal.objects.create(name="lion", sound="roar") # lion.pk is guaranteed to always be 1 self.assertEqual(lion.pk, 1)
0π
If you use any sort of "flush", keep in mind that if youβve automated user creation or any other sort of data insert, itβll be deleted too.
An easy way to solve this problem without flushing is to get the ID of the first object during the test and increment from there.
Test1:
assertEqual(foo.id, 1)
assertEqual(bar.id, 2)
Test2:
start_id = Foo.objects.first().id
assertEqual(foo.id, start_id)
assertEqual(bar.id, (start_id + 1))
- Django β Change a ForeignKey relation to OneToOne
- Django makemessages not creating newly added languages