Skip to content

Instantly share code, notes, and snippets.

@mrroot5
Created April 16, 2020 11:02
Show Gist options
  • Save mrroot5/c0204b3285606652d5283b273907260a to your computer and use it in GitHub Desktop.
Save mrroot5/c0204b3285606652d5283b273907260a to your computer and use it in GitHub Desktop.
Crear un model temporal para test en Django: Keyworkds: Django, test, model test, modelo temporal django test

Intro

Sacado de aquí: https://stackoverflow.com/a/503435/3377046.

Post

You can put your tests in a tests/ subdirectory of the app (rather than a tests.py file), and include a tests/models.py with the test-only models.

Then provide a test-running script (example) that includes your tests/ "app" in INSTALLED_APPS. (This doesn't work when running app tests from a real project, which won't have the tests app in INSTALLED_APPS, but I rarely find it useful to run reusable app tests from a project, and Django 1.6+ doesn't by default.)

(NOTE: The alternative dynamic method described below only works in Django 1.1+ if your test case subclasses TransactionTestCase - which slows down your tests significantly - and no longer works at all in Django 1.7+. It's left here only for historical interest; don't use it.)

At the beginning of your tests (i.e. in a setUp method, or at the beginning of a set of doctests), you can dynamically add "myapp.tests" to the INSTALLED_APPS setting, and then do this:

from django.core.management import call_command
from django.db.models import loading
loading.cache.loaded = False
call_command('syncdb', verbosity=0)

Then at the end of your tests, you should clean up by restoring the old version of INSTALLED_APPS and clearing the app cache again.

This class encapsulates the pattern so it doesn't clutter up your test code quite as much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment