[Answered ]-Proper way to manage fixtures in django

2๐Ÿ‘

โœ…

If you think about the cleanest way to define test data for your tests, I would like to recommend you read about django-any application:

django-any the explicit replacement for old-style, big and error-prone
implicit fixture files.

django-any allows to specify only fields important for test, and fill
rest by random with acceptable values.

It makes tests clean and easy to undestood, without reading fixture
files.

from django_any import any_model, WithTestDataSeed

class TestMyShop(TestCase):
    def test_order_updates_user_account(self):
        account = any_model(Account, amount=25, user__is_active=True)
        order = any_model(Order, user=account.user, amount=10)
        order.proceed()

        account = Account.objects.get(pk=account.pk)
        self.assertEquals(15, account.amount)

The same approach available for forms also (django_any.any_form)

This solution is helpful for avoiding to keep extra data in you DB while your tests are executing.

๐Ÿ‘คramusus

Leave a comment