How to create a model dynamically just for testing in djanjo

+1 vote

I have a Django app that requires a settings attribute in the form of:

RELATED_MODELS = ('appname1.modelname1.attribute1',
                  'appname1.modelname2.attribute2', 
                  'appname2.modelname3.attribute3', ...)

Then hooks their post_save signal to update some other fixed model depending on the attributeN defined.

I would like to test this behaviour and tests should work even if this app is the only one in the project (except for its own dependencies, no other wrapper app need to be installed). How can I create and attach/register/activate mock models just for the test database? (or is it possible at all?)

Jun 29, 2020 in Python by kartik
• 37,510 points
923 views

1 answer to this question.

0 votes

Hello @kartik,

The basic idea is, make your tests an app, and add your tests to INSTALLED_APPS.

Here's an example:

$ ls common
__init__.py   admin.py      apps.py       fixtures      models.py     pagination.py tests         validators.py views.py

$ ls common/tests
__init__.py        apps.py            models.py          serializers.py     test_filter.py     test_pagination.py test_validators.py views.py

And I have different settings for different purposes, namely:

  • settings/default.py: base settings file
  • settings/production.py: for production
  • settings/development.py: for development
  • settings/testing.py: for testing.

And in settings/testing.py, you can modify INSTALLED_APPS:

settings/testing.py:

from default import *

DEBUG = True

INSTALLED_APPS += ['common', 'common.tests']

And make sure that you have set a proper label for your tests app, namely,

common/tests/apps.py

from django.apps import AppConfig


class CommonTestsConfig(AppConfig):
    name = 'common.tests'
    label = 'common_tests'

common/tests/__init__.py, set up proper AppConfig.

default_app_config = 'common.tests.apps.CommonTestsConfig'

Then, generate db migration by

python manage.py makemigrations --settings=<your_project_name>.settings.testing tests

Finally, you can run your test with param --settings=<your_project_name>.settings.testing.

If you use py.test, you can even drop a pytest.ini file along with django's manage.py.

py.test

[pytest]
DJANGO_SETTINGS_MODULE=kungfu.settings.testing

Hope it helps!

Thanks!

answered Jun 29, 2020 by Niroj
• 82,880 points

Related Questions In Python

0 votes
1 answer

In python how to test a string for a substring

if "ABCD" in "xxxxABCDyyyy": This can be used ...READ MORE

answered Oct 24, 2018 in Python by Priyaj
• 58,090 points
566 views
0 votes
1 answer

How to create and read from a temporary file in Python?

Hi, there is a very simple solution ...READ MORE

answered Jan 29, 2019 in Python by Nymeria
• 3,560 points
1,778 views
0 votes
1 answer

How to create a GUID/UUID in Python

The uuid module, in Python 2.5 and ...READ MORE

answered Jan 29, 2019 in Python by SDeb
• 13,300 points
1,540 views
0 votes
1 answer

how to check for exceptions in a file?

try { if (!file.exists("TextFile1.txt")) throw ...READ MORE

answered Mar 20, 2019 in Python by Mohammad
• 3,230 points
435 views
0 votes
1 answer

How to temporarily disable a foreign key constraint in MySQL?

Hello @kartik, To turn off foreign key constraint ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
2,080 views
0 votes
1 answer

How do I use Django templates without the rest of Django?

Hello @kartik, Let's say you have this important ...READ MORE

answered Jun 23, 2020 in Python by Niroj
• 82,880 points
1,227 views
0 votes
1 answer

How to create a generic serializer with a dynamic model in Meta?

Hello @kartik, You can do that by following: serializers.py class ...READ MORE

answered Aug 5, 2020 in Python by Niroj
• 82,880 points
3,111 views
0 votes
1 answer

How to create a zip archive of a directory in Python?

Hello @kartik, The easiest way is to use shutil.make_archive. ...READ MORE

answered May 11, 2020 in Python by Niroj
• 82,880 points
439 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP