如何启动django可重用应用程序的测试? [英] How to launch tests for django reusable app?
问题描述
我可以为我的可重用Django应用程序启动测试,而不将此应用程序并入项目?
我的应用程序使用某些型号,因此有必要提供(TEST_)DATABASE _ *
设置。我应该在哪里存放它们以及如何启动测试?
对于Django项目,我可以使用 manage.py test
;当我使用我的独立应用程序使用 django-admin.py test
时,我得到:
错误:无法导入设置
因为环境变量
DJANGO_SETTINGS_MODULE未定义。
什么是最好的做法在这里?
我已经结束了这样的解决方案(它受到django投票中的解决方案的启发): / p>
创建文件例如。 'runtests.py'in test dir contains:
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)
如果失败:
sys.exit(failures)
它允许通过 python runtests运行测试.py
命令。
它不需要安装的依赖项(例如,buildout),并且当应用程序被并入到更大的项目中时,它不会损害测试运行。
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.
这篇关于如何启动django可重用应用程序的测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!