#!/usr/bin/env python import unittest, os, pprint, types import coverage, coverage_color 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.utils import create_test_db, destroy_test_db from django.test.simple import build_suite, build_test def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): setup_test_environment() settings.DEBUG = False suite = unittest.TestSuite() coverage.start() 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 create_test_db(verbosity, autoclobber=not interactive) result = unittest.TextTestRunner(verbosity=verbosity).run(suite) if settings.COVERAGE_MODULES: coverage.stop() coveragedir = './build/coverage' if hasattr(settings, 'COVERAGE_DIR'): coveragedir = settings.COVERAGE_DIR if not os.path.exists(coveragedir): os.makedirs(coveragedir) modules = [] for module_string in settings.COVERAGE_MODULES: module = __import__(module_string, globals(), locals(), [""]) modules.append(module) f,s,m,mf = coverage.analysis(module) fp = file(os.path.join(coveragedir, module_string + ".html"), "wb") coverage_color.colorize_file(f, outstream=fp, not_covered=mf) fp.close() coverage.the_coverage.report(modules, show_missing=False) coverage.erase() destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)