13

I need to do some unit tests in my Django project. The problem is that almost every use case depends on prepopulated database objects.

For example, I want to create a product and test, if there were all pre_save signals successful.

from django.contrib.auth.models import User
from django.test import TestCase

from .models import Product


class ProductTestCase(TestCase):
    def setUp(self):
        self.user = User.objects.create(username='test_user')
        self.product = Product.objects.create(name='Test product',user=self.user)


    def test_product_exists(self):
        self.assertIsNotNone(self.product)

    def product_is_active_by_default(self):
        ...

I can't do that because product has to have User object related. But I can't create a User object because User has to have related plan object. There are multiple plans in my production database from which one is default but there are no plans inside test database.

So to be able to do unit tests I need to prepopulate test database with multiple objects from multiple apps.

How can I do that?

Milano
  • 18,048
  • 37
  • 153
  • 353
  • 1
    I want to confirm @Gabriel Muj answer's as the best one in the long run (I can't post comments yet, so I put it as an answer). I've used hardcoded data for my tests, then used it with factories, which is a great improvement (factory boy), but, combining it with faker to create the data of the tested models is a huge plus. Simplifies the process, the testing logic, testing writing time, and maintainability. I just leave here the [article](https://hacksoft.io/improve-your-tests-django-fakes-and-factories/) that helped me with that combination. – Rafael Karosuo Nov 21 '20 at 19:50

2 Answers2

24

you can simply use django fixtures for that :-)

first populate a sample db with data then export data with python manage.py dumpdata

then in one of your apps create a directory named fixtures and put exported json file there (named tests.json or something else)

in your test class load fixtures like this

class ProductTestCase(TestCase):
    fixtures = ['tests.json', ]

checkout django docs

PS: checkout factory boy too (@Gabriel Muj) answer

aliva
  • 5,450
  • 1
  • 33
  • 44
11

I don't recommend using fixture since you will need to maintain them each time you make changes to the model. Here is a better approach on creating objects for tests by using this library https://factoryboy.readthedocs.io/en/latest/ which is more flexible.

Gabriel Muj
  • 3,682
  • 1
  • 19
  • 28