Skip to content

Instantly share code, notes, and snippets.

@leah
Created March 18, 2009 05:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leah/80957 to your computer and use it in GitHub Desktop.
Save leah/80957 to your computer and use it in GitHub Desktop.
import unittest
from django.conf import settings
from django.db.models import get_app, get_apps
from django.test.utils import setup_test_environment, teardown_test_environment
from django.test.simple import build_suite
from django.db.backends.creation import TEST_DATABASE_PREFIX
import couchdb
def create_test_couchdb():
server = couchdb.Server(settings.COUCHDB_HOST)
test_database_name = TEST_DATABASE_PREFIX + settings.COUCHDB_NAME
server.create(test_database_name)
def destroy_test_couchdb(old_database_name):
server = couchdb.Server(settings.COUCHDB_HOST)
test_database_name = settings.COUCHDB_NAME
settings.COUCHDB_NAME = old_database_name
del server[test_database_name]
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]):
"""
Copied directly from django.test.simple.run_tests with the minor
addition of a test CouchDB setup and teardown.
See: django.test.simple.run_tests
"""
setup_test_environment()
settings.DEBUG = False
suite = unittest.TestSuite()
if test_labels:
for label in test_labels:
if '.' in label:
suite.addTest(build_test(label))
else:
app = get_app(label)
suite.addTest(build_suite(app))
else:
for app in get_apps():
suite.addTest(build_suite(app))
for test in extra_tests:
suite.addTest(test)
old_name = settings.DATABASE_NAME
from django.db import connection
connection.creation.create_test_db(verbosity, autoclobber=not interactive)
# Test CouchDB setup
old_couch_name = settings.COUCHDB_NAME
create_test_couchdb()
result = unittest.TextTestRunner(verbosity=verbosity).run(suite)
# Test CouchDB teardown
destroy_test_couchdb(old_couch_name)
connection.creation.destroy_test_db(old_name, verbosity)
teardown_test_environment()
return len(result.failures) + len(result.errors)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment