Search code examples
pythondjangotestingdjango-modelsdjango-testing

Test specific models in Django


Is it possible to have a set of models just for testing purposes? The idea is that I've written an app that contains some helper abstract model HelperBase. Now I'd like to provide some models that would inherit from it in order to test it, say DerivedTest1, DerivedTest2. However I wouldn't really like those test models to appear in the production database in the end. I just want their tables to be constructed in the test database. Is it possible and if so - how to do it? I've already tried creating models in the tests.py file but this doesn't seem to work.


Solution

  • You could try creating a whole new app that you only use on your development server.

    E.g., if your app is called myapp you would call your testing app myapp_test.

    Then in myapp_test's models.py you would from myapp import models and then subclass your models in there.

    Then in your settings.py you either just try and remember to comment out the myapp_test application from INSTALLED_APPS when deploying to your production server. Or you can use the local_settings.py methodology to only have the myapp_test included in INSTALLED_APPS on your test machine.