Django tests with MongoEngine
I took me hours to make this work finally
In this post, I'm assuming you know how to run django 1.7 with mongoengine 0.8.7
If you wanna have tests in a folder instead of a single file please check my other post
http://techamad.blogspot.com.es/2015/05/django-mongoengine-tests-directoryfolder.html
tests.py
from django.test import TestCase
from django.test.simple import DjangoTestSuiteRunner
class NoSQLTestRunner(DjangoTestSuiteRunner):
def setup_databases(self):
pass
def teardown_databases(self, *args):
pass
class NoSQLTestCase(TestCase):
def _fixture_setup(self):
pass
def _fixture_teardown(self):
pass
#Testing the home page
class HomePageInitTestCase(TestCase):
def test_page(self):
response = self.client.get('/')
self.assertEqual(response.status_code, 200, 'error in status_code of the homepage init test')
def _fixture_setup(self):
return
def _fixture_teardown(self):
return
setting.py
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
AUTHENTICATION_BACKENDS = (
'mongoengine.django.auth.MongoEngineBackend',
)
LOGIN_URL = '/login'
#Needed for the tests
TEST_RUNNER = 'yourapp.tests.NoSQLTestRunner'
from mongoengine import connect
the_db = connect("mydatabasename")
AUTH_USER_MODEL = 'mongo_auth.MongoUser'
MONGOENGINE_USER_DOCUMENT = 'mongoengine.django.auth.User'
SESSION_ENGINE = 'mongoengine.django.sessions'
SESSION_SERIALIZER = 'mongoengine.django.sessions.BSONSerializer'
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.7/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'asdfasdfsadfasdfasdfasdfasdfasdfasdfdasfasdfa71j*^u_b+2'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = []
TEMPLATE_DIRS = (
BASE_DIR+'/templates',
)
MEDIA_ROOT = BASE_DIR+'/media/'
MEDIA_URL = '/media/'
# Application definition
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'mongoengine.django.mongo_auth',
'myapp',
)
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
TEMPLATE_CONTEXT_PROCESSORS=(
"django.contrib.auth.context_processors.auth",
"django.core.context_processors.debug",
"django.core.context_processors.i18n",
"django.core.context_processors.media",
"django.core.context_processors.static",
"django.core.context_processors.tz",
"django.contrib.messages.context_processors.messages",
"django.core.context_processors.request"
)
ROOT_URLCONF = 'myapp.urls'
WSGI_APPLICATION = 'myapp.wsgi.application'
# Database
# https://docs.djangoproject.com/en/1.7/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.dummy',
}
}
# Internationalization
# https://docs.djangoproject.com/en/1.7/topics/i18n/
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_L10N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.7/howto/static-files/
STATIC_URL = '/static/'
References:
http://staltz.com/djangoconfi-mongoengine/#/18
http://toastdriven.com/blog/2011/apr/10/guide-to-testing-in-django/
To see an actual app please refer to my OnToology application on Github:
https://github.com/OnToology/OnToology
Comments
Post a Comment