Can I launch tests for my reusable Django app without incorporating this app into a project?
My app uses some models, so it is necessary to provide (TEST_)DATABASE_*
settings. Where should I store them and how should I launch tests?
For a Django project, I can run tests with manage.py test
; when I use django-admin.py test
with my standalone app, I get:
Error: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.
What are the best practises here?
I've ended with such solution (it was inspired by solution found in django-voting):
Create file eg. 'runtests.py' in tests dir containing:
import os, sys
from django.conf import settings
DIRNAME = os.path.dirname(__file__)
settings.configure(DEBUG = True,
DATABASE_ENGINE = 'sqlite3',
DATABASE_NAME = os.path.join(DIRNAME, 'database.db'),
INSTALLED_APPS = ('django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.admin',
'myapp',
'myapp.tests',))
from django.test.simple import run_tests
failures = run_tests(['myapp',], verbosity=1)
if failures:
sys.exit(failures)
It allows to run tests by python runtests.py
command.
It doesn't require installed dependencies (eg. buildout) and it doesn't harm tests run when app is incorporated into bigger project.