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
- View the SQL queries for Django queryset delete
- How can I reset a Django test database id's after each test?
- Psycopg2.OperationalError: FATAL: password authentication failed for user "<my UNIX user>"
Source:stackexchange.com