[Fixed]-How to prepopulate test database by necessary data?

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

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

Leave a comment