Search code examples
pythondjangotestingdjango-testing

Runserver in "test-mode" with test databases


I'm trying to create tests for my Django application but I'm having some trouble creating a test database.

I'd like to keep the existing structure while entering new curated test-information, creating test users, uploading test content, etc. Which I can then populate a test database with so that I have curated data on which I can test edge-cases.

Creating a test database seems simple, just run python manage.py test --keepdb. Getting entries into it seems more difficult.

Is it possible to run Django in "test mode" with the test database being used so that I can use the website UI to enter all the data, or is there some other better way to do it entirely?


Solution

  • I assume you mean testing with unit tests?

    Usually you fill the database with fixtures, or some other test data that is populated into the database as a part of the test itself.

    1. Django fixtures: https://code.djangoproject.com/wiki/Fixtures
    2. Fixtureless is a good option, https://pypi.org/project/django-fixtureless/
    3. Factory Boy http://factoryboy.readthedocs.io/en/latest/

    These options allow you to fill your database with fake or static data to use in your tests.