def build_suite(self, test_labels, extra_tests=None, **kwargs): 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(): app_name = guess_app(app) for pattern in getattr(settings, 'TEST_WHITELIST', ('%s.*'%__name__.split('.')[0],)): if fnmatch(app_name, pattern): suite.addTest(build_suite(app)) break for name in getattr(settings, 'TEST_EXTRA', ()): mod = import_module(name + '.' + TEST_MODULE) extra_suite = unittest.defaultTestLoader.loadTestsFromModule(mod) suite.addTest(extra_suite) if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase,))
def run_tests(test_labels=APPS, verbosity=1, interactive=True, extra_tests=[]): """Hack on top of django.test.simple.run_tests to use the XML test runner provided by Bitten""" 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) suite = reorder_suite(suite, (TestCase,)) old_name = settings.DATABASE_NAME from django.db import connection connection.creation.create_test_db(verbosity, autoclobber=not interactive) result = XMLTestRunner(stream=sys.stdout, xml_stream=open("test-results.xml", "w")).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() return result
def run_the_old_way(extra_tests, kwargs, test_labels, verbosity): from django.test.simple import build_suite, build_test, get_app, get_apps, \ setup_test_environment, teardown_test_environment 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) suite = reorder_suite(suite, (TestCase,)) old_name = settings.DATABASE_NAME from django.db import connection connection.creation.create_test_db(verbosity, autoclobber=False) result = DjangoTeamcityTestRunner().run(suite, **kwargs) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def build_suite(self, test_labels, extra_tests=None, **kwargs): suite = unittest.TestSuite() if test_labels: for label in test_labels: if '.' in label: appname, test = label.split('.', 1) else: appname, test = label, '' def filter_test(testcase, testprefix=label): testname = "%s.%s.%s" % (testcase.__class__.__module__, testcase.__class__.__name__, testcase) return testname.startswith(testprefix) app = get_app(appname) suite.addTest(simple.build_suite(app)) self.filter_suite(suite, filter_test) else: for appname in settings.OUR_APPS: app = get_app(appname, emptyOK=True) if app is None: continue suite.addTest(simple.build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) return simple.reorder_suite(suite, (testcases.TestCase,))
def handle(self, *fixture_labels, **options): from django.core.management import call_command from django.db import connection from django.conf import settings settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES) + ['djamocha.middleware.DjamochaTestMiddleware'] verbosity = int(options.get('verbosity')) interactive = options.get('interactive') addrport = options.get('addrport') # Create a test database. db_name = connection.creation.create_test_db(verbosity=verbosity, autoclobber=not interactive) # Import the fixture data into the test database. call_command('loaddata', *fixture_labels, **{'verbosity': verbosity}) runner = CLIRunner( stderr=original_stderr, stdout=original_stdout ) for app in get_apps(): build_suite(app) # Run the development server. Turn off auto-reloading because it causes # a strange error -- it causes this handle() method to be called # multiple times. shutdown_message = '\nServer stopped.\nNote that the test database, %r, has not been deleted. You can explore it on your own.' % db_name call_command('runserver', addrport=addrport, shutdown_message=shutdown_message, use_reloader=False, use_ipv6=options['use_ipv6'])
def build_suite(self, test_labels, extra_tests=None, **kwargs): 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: # Get ignored tests ignored_labels = getattr(settings, "IGNORE_TESTS", ()) ignored_apps = [] for ilabel in ignored_labels: if "." in ilabel: ilabel = ilabel.split(".")[-1] try: app = get_app(ilabel) except Exception as e: print e ignored_apps.append(app) for app in get_apps(): if app not in ignored_apps: suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase, ))
def build_suite(self, test_labels, extra_tests=None, **kwargs): 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: print "Excluding apps: %s" % ', '.join( settings.APPS_NOT_FOR_TESTING) for app in get_apps(): if app.__package__ in settings.APPS_NOT_FOR_TESTING or \ app.__name__ in settings.APPS_NOT_FOR_TESTING: continue print "Will test %s" % app.__name__ suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: print "Adding extra test %s" % test suite.addTest(test) return reorder_suite(suite, (TestCase, ))
def build_suite(self, test_labels, extra_tests=None, **kwargs): 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(): pkg = app.__name__ if self.is_excluded(pkg): print "Deleting %s" % pkg #del suite._tests[key] else: print "Keeping %s" % pkg suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: pass #suite.addTest(test) return reorder_suite(suite, (TestCase,))
def build_suite(self, test_labels, extra_tests=None, **kwargs): suite = unittest.TestSuite() if test_labels: print test_labels for label in test_labels: print label if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: print 'Filtering applications to test...' for app in get_apps(): if isinstance( app.__package__, basestring) and app.__package__.startswith('esp.'): print ' Adding: %s' % app.__package__ suite.addTest(build_suite(app)) else: print ' Skipping: %s' % app.__package__ if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase, ))
def run_tests(test_labels, verbosity=1, interactive=True, extra_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) result = XMLTestRunner(verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): # Same as django.test.simple.run_tests but return the result object. # http://code.djangoproject.com/browser/django/trunk/django/test/simple.py#L149 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) suite = reorder_suite(suite, (TestCase,)) old_name = settings.DATABASE_NAME from django.db import connection connection.creation.create_test_db(verbosity, autoclobber=not interactive) result = unittest.TextTestRunner(verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() return result
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """Run the unit tests without using the ORM. """ setup_test_environment() settings.DEBUG = False settings.DATABASE_SUPPORTS_TRANSACTIONS = 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) suite = reorder_suite(suite, (TestCase,)) runner = unittest.TextTestRunner(verbosity=verbosity) result = runner.run(suite) teardown_test_environment() return len(result.failures) + len(result.errors)
def handle(self, *fixture_labels, **options): from django.core.management import call_command from django.db import connection from django.conf import settings settings.MIDDLEWARE_CLASSES = list(settings.MIDDLEWARE_CLASSES) + [ 'djamocha.middleware.DjamochaTestMiddleware' ] verbosity = int(options.get('verbosity')) interactive = options.get('interactive') addrport = options.get('addrport') # Create a test database. db_name = connection.creation.create_test_db( verbosity=verbosity, autoclobber=not interactive) # Import the fixture data into the test database. call_command('loaddata', *fixture_labels, **{'verbosity': verbosity}) runner = CLIRunner(stderr=original_stderr, stdout=original_stdout) for app in get_apps(): build_suite(app) # Run the development server. Turn off auto-reloading because it causes # a strange error -- it causes this handle() method to be called # multiple times. shutdown_message = '\nServer stopped.\nNote that the test database, %r, has not been deleted. You can explore it on your own.' % db_name call_command('runserver', addrport=addrport, shutdown_message=shutdown_message, use_reloader=False, use_ipv6=options['use_ipv6'])
def build_suite(self, test_labels, extra_tests=None, **kwargs): 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: # Get ignored tests ignored_labels = getattr(settings, "IGNORE_TESTS", ()) ignored_apps = [] for ilabel in ignored_labels: if "." in ilabel: ilabel = ilabel.split(".")[-1] try: app = get_app(ilabel) except Exception as e: print e else: ignored_apps.append(app) for app in get_apps(): if app not in ignored_apps: suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase,))
def safe_build_suite(self, test_labels, extra_tests=None, **kwargs): suite = unittest.TestSuite() if test_labels: for label in test_labels: try: if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) except Exception: log.warning("Could not add test for label: %s" %label) else: for app in get_apps(): try: suite.addTest(build_suite(app)) except Exception: log.warning("Could not add tests for app: %s" %app) if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase,))
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """Run the unit tests without using the ORM. """ setup_test_environment() settings.DEBUG = False settings.DATABASE_SUPPORTS_TRANSACTIONS = 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) suite = reorder_suite(suite, (TestCase, )) runner = unittest.TextTestRunner(verbosity=verbosity) result = runner.run(suite) teardown_test_environment() return len(result.failures) + len(result.errors)
def build_suite(self, test_labels, extra_tests=None, **kwargs): # XXX: We shouldn't need to hook this if Mule can handle the shortname.TestCase format suite = unittest2.TestSuite() if test_labels: for label in test_labels: if '.' in label: suite.addTest(get_test_by_name(label, self.loader)) else: app = get_app(label) suite.addTest(build_suite(app)) else: for app in get_apps(): suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) new_suite = unittest2.TestSuite() for test in reorder_suite(suite, (unittest.TestCase,)): # XXX: Doctests (the way we do it currently) do not work if isinstance(test, DocTestCase): continue if self.include_testcases and not any(isinstance(test, c) for c in self.include_testcases): continue if self.exclude_testcases and any(isinstance(test, c) for c in self.exclude_testcases): continue new_suite.addTest(test) return reorder_suite(new_suite, (unittest.TestCase,))
def build_suite(self, test_labels, extra_tests=None, **kwargs): 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: # Get ignored tests ignored_labels = getattr(settings, "IGNORE_TESTS", ()) ignored_apps = [] for ilabel in ignored_labels: if "." in ilabel: ilabel = ilabel.split(".")[-1] app = get_app(ilabel) ignored_apps.append(app) for app in get_apps(): if app not in ignored_apps: suite.addTest(build_suite(app)) suite = reorder_suite(suite, (TestCase, )) # signals.build_suite.send(sender=self, suite=suite) return suite
def build_suite(self, test_labels, extra_tests=None, **kwargs): 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: # Get ignored tests ignored_labels = getattr(settings, "IGNORE_TESTS", ()) ignored_apps = [] for ilabel in ignored_labels: if "." in ilabel: ilabel = ilabel.split(".")[-1] app = get_app(ilabel) ignored_apps.append(app) for app in get_apps(): if app not in ignored_apps: suite.addTest(build_suite(app)) suite = reorder_suite(suite, (TestCase,)) # signals.build_suite.send(sender=self, suite=suite) return suite
def run_tests(self, test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ setup_test_environment() settings.DEBUG = False verbose = getattr(settings, 'TEST_OUTPUT_VERBOSE', False) descriptions = getattr(settings, 'TEST_OUTPUT_DESCRIPTIONS', False) output = getattr(settings, 'TEST_OUTPUT_DIR', '.') exclude_apps = getattr(settings, 'EXCLUDE_APPS', None) 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(): exclude = False if exclude_apps: for e_app in exclude_apps: if app.__name__.startswith(e_app): exclude = True if not exclude: suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) old_config = self.setup_databases() result = xmlrunner.XMLTestRunner(verbose=verbose, descriptions=descriptions, output=output, resultclass=self.resultclass).run(suite) self.teardown_databases(old_config) teardown_test_environment() return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None): """ Set `TEST_RUNNER` in your settings with this routine in order to scaffold test spatial databases correctly for your GeoDjango models. For more documentation, please consult the following URL: http://geodjango.org/docs/testing.html. """ from django.conf import settings from django.db import connection from django.db.models import get_app, get_apps from django.test.simple import build_suite, build_test, reorder_suite, TestCase from django.test.utils import setup_test_environment, teardown_test_environment # The `create_test_spatial_db` routine abstracts away all the steps needed # to properly construct a spatial database for the backend. from django.contrib.gis.db.backend import create_test_spatial_db # Setting up for testing. setup_test_environment() settings.DEBUG = False old_name = settings.DATABASE_NAME # Creating the test spatial database. create_test_spatial_db(verbosity=verbosity, autoclobber=not interactive) # The suite may be passed in manually, e.g., when we run the GeoDjango test, # we want to build it and pass it in due to some customizations. Otherwise, # the normal test suite creation process from `django.test.simple.run_tests` # is used to create the test suite. if suite is None: 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) suite = reorder_suite(suite, (TestCase, )) # Executing the tests (including the model tests), and destorying the # test database after the tests have completed. result = unittest.TextTestRunner(verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() # Returning the total failures and errors return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ This is basically a copy of the default django run_tests method, except with the addition of the coverage report as documented at http://siddhi.blogspot.com/2007/04/code-coverage-for-your-django-code.html """ if hasattr(settings, 'TEST_DATABASE_ENGINE'): settings.DATABASE_ENGINE = settings.TEST_DATABASE_ENGINE 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 create_test_db(verbosity, autoclobber=not interactive) coverage.start() print("Running tests ...") result = unittest.TextTestRunner(verbosity=verbosity).run(suite) print("Done running tests.") coverage.stop() if not os.path.exists(settings.COVERAGE_DIR): os.makedirs(settings.COVERAGE_DIR) 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(settings.COVERAGE_DIR, module_string + ".html"), "wb") coverage_color.colorize_file(f, outstream=fp, not_covered=mf) fp.close() coverage.report(modules) coverage.erase() destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=False, extra_tests=[], **kwargs): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ TeamcityServiceMessages(sys.stdout).testMatrixEntered() if VERSION[1] > 1: return DjangoTeamcityTestRunner().run_tests(test_labels, extra_tests=extra_tests, **kwargs) 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) suite = reorder_suite(suite, (TestCase,)) old_name = settings.DATABASE_NAME from django.db import connection connection.creation.create_test_db(verbosity, autoclobber=False) result = DjangoTeamcityTestRunner().run(suite, **kwargs) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ setup_test_environment() settings.DEBUG = False suite = unittest.TestSuite() modules_to_cover = [] # if passed a list of tests... 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)) # ...otherwise use all installed else: for app in get_apps(): # skip apps named "Django" because they use a database if not app.__name__.startswith('django'): # get the actual app name app_name = app.__name__.replace('.models', '') # get a list of the files inside that module files = glob('%s/*.py' % app_name) # remove models because we don't use them, stupid new_files = [i for i in files if not i.endswith('models.py')] modules_to_cover.extend(new_files) # actually test the file suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) result = unittest.TextTestRunner(verbosity=verbosity).run(suite) teardown_test_environment() return len(result.failures) + len(result.errors)
def run(request): """ Runs all the tests. """ # Some stuff. verbosity=1 interactive=True extra_tests=[] # Setup the response. response = HttpResponse() # Get the installed apps for this project. installed_apps = settings.INSTALLED_APPS # The list of applications which will be tested. test_labels = [] # Populate the test_labels list with the POST data. for item in request.POST.getlist('apps'): test_labels.append(settings.INSTALLED_APPS[int(item)]) try: setup_test_environment() settings.DEBUG = False settings.DATABASE_ENGINE = 'sqlite3' 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) suite = reorder_suite(suite, (TestCase,)) old_name = settings.DATABASE_NAME from django.db import connection connection.creation.create_test_db(verbosity, autoclobber=not interactive) result = HTMLTestRunner.HTMLTestRunner(stream=response, verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() except Exception, e: from ipdb import set_trace; set_trace() raise e
def run_tests(test_labels, verbosity = 1, interactive = True, extra_tests=[]): """ This is basically a copy of the default django run_tests method, except with the addition of the coverage report as documented at http://siddhi.blogspot.com/2007/04/code-coverage-for-your-django-code.html """ if hasattr(settings, 'TEST_DATABASE_ENGINE'): settings.DATABASE_ENGINE = settings.TEST_DATABASE_ENGINE 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 create_test_db(verbosity, autoclobber=not interactive) coverage.start() print "Running tests ..." result = unittest.TextTestRunner(verbosity=verbosity).run(suite) print "Done running tests." coverage.stop() if not os.path.exists(settings.COVERAGE_DIR): os.makedirs(settings.COVERAGE_DIR) 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(settings.COVERAGE_DIR, module_string + ".html"), "wb") coverage_color.colorize_file(f, outstream=fp, not_covered=mf) fp.close() coverage.report(modules) coverage.erase() destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None): """ Set `TEST_RUNNER` in your settings with this routine in order to scaffold test spatial databases correctly for your GeoDjango models. For more documentation, please consult the following URL: http://geodjango.org/docs/testing.html. """ from django.conf import settings from django.db import connection from django.db.models import get_app, get_apps from django.test.simple import build_suite, build_test from django.test.utils import setup_test_environment, teardown_test_environment # The `create_test_spatial_db` routine abstracts away all the steps needed # to properly construct a spatial database for the backend. from django.contrib.gis.db.backend import create_test_spatial_db # Setting up for testing. setup_test_environment() settings.DEBUG = False old_name = settings.DATABASE_NAME # Creating the test spatial database. create_test_spatial_db(verbosity=verbosity, autoclobber=not interactive) # The suite may be passed in manually, e.g., when we run the GeoDjango test, # we want to build it and pass it in due to some customizations. Otherwise, # the normal test suite creation process from `django.test.simple.run_tests` # is used to create the test suite. if suite is None: 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) suite = reorder_suite(suite, (TestCase,)) # Executing the tests (including the model tests), and destorying the # test database after the tests have completed. result = unittest.TextTestRunner(verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() # Returning the total failures and errors return len(result.failures) + len(result.errors)
def run_tests(self, test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ setup_test_environment() settings.DEBUG = False verbose = getattr(settings, 'TEST_OUTPUT_VERBOSE', False) descriptions = getattr(settings, 'TEST_OUTPUT_DESCRIPTIONS', False) output = getattr(settings, 'TEST_OUTPUT_DIR', '.') 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) suite = reorder_suite(suite, (TestCase,)) old_config = self.setup_databases() result = xmlrunner.XMLTestRunner( verbose=verbose, descriptions=descriptions, output=output).run(suite) self.teardown_databases(old_config) teardown_test_environment() return len(result.failures) + len(result.errors)
def build_tests(self): """ Build tests for inclusion in suite from resolved packages for <= 1.8 TODO: Cleanup/simplify this method, flow too complex, too much duplication. """ from django.core.exceptions import ImproperlyConfigured from django.test.simple import build_suite, build_test try: from django.apps import apps get_app = apps.get_app_config except ImportError: from django.db.models import get_app tests = [] packages = [self.options['label'], ] if \ self.options['label'] else self.packages for package in packages: try: if not self.options['autoreload']: if self.options['label']: try: tests.append(build_test(package)) except (ImproperlyConfigured, ValueError) as e: self.handle_label_exception(e) else: app = get_app(package) tests.append(build_suite(app)) else: # Wait for exceptions to be resolved. exception = None while True: try: if self.options['label']: try: tests.append(build_test(package)) except (ImproperlyConfigured, ValueError) as e: self.handle_label_exception(e) else: app = get_app(package) tests.append(build_suite(app)) break except LabelException: raise except Exception as e: if exception != str(e): traceback.print_exc() exception = str(e) time.sleep(1) except ImproperlyConfigured as e: log.info("Warning: %s" % traceback.format_exc()) except ImportError as e: log.info("Warning: %s" % traceback.format_exc()) return tests
def build_suite(self, suite, **kwargs): if self.test_labels: for label in self.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))
def build_suite(self, suite, **kwargs): if self.test_labels: for label in self.test_labels: if '.' in label: suite.addTest(build_test(label)) else: try: app = get_app(label) suite.addTest(build_suite(app)) except ImproperlyConfigured: pass else: for app in get_apps(): suite.addTest(build_suite(app))
def build_suite(self, test_labels, extra_tests=None, **kwargs): if not test_labels: return super(TeleduTestRunner, self).build_suite(test_labels, extra_tests, **kwargs) suite = TestSuite() for label in test_labels: parts = label.split('.') if parts[0].lower() == 'teledu': if len(parts) < 2: suite.addTest(unittest.TestLoader().discover(start_dir = self.testPath, pattern = 'when*.py')) else: suite.addTest(self._loadTestsFromLabel(parts[1])) else: if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) return suite
def build_suite(self, test_labels, extra_tests=None, **kwargs): suite = None # Do default test loading if no test labels specfied if not test_labels: suite = unittest.TestSuite() for app in get_apps(): suite.addTest(build_suite(app)) # Then intelligent test find from top directory suite.addTest(unittest.defaultTestLoader.discover('.')) # If 'projectapps' in test_labels then find only the tests in my # project, but find all of them if 'projectapps' in test_labels: suite = unittest.TestSuite() suite.addTest(unittest.defaultTestLoader.discover('.')) # Else can only handle a single project name or a series of tests elif test_labels: root = '.' # Loads tests from dotted tests names suite = unittest.defaultTestLoader.loadTestsFromNames(test_labels) # if single named module has no tests, do discovery within it if not suite.countTestCases() and len(test_labels) == 1: suite = None root = import_module(test_labels[0]).__path__[0] suite = unittest.defaultTestLoader.discover(root) # Default DjangoTestSuiteRunner behavior if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase,))
def get_tests(self): if not hasattr(self, "_tests"): self._suite = unittest.TestSuite() self._suite.addTest(build_suite(self.module)) self._suite = reorder_suite(self._suite, (TestCase,)) self._tests = [Test(test, self.label) for test in self._suite._tests] return self._tests
def build_suite(self, test_labels, extra_tests=None, **kwargs): suite = unittest.TestSuite() if test_labels: for label in test_labels: if "." in label: from django.test.simple import build_test suite.addTest(build_test(label)) else: sub_suite = self.find_tests_and_apps(label) suite.addTest(sub_suite) else: from django.db.models import get_apps for app in get_apps(): from django.test.simple import build_suite suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) from django.test.simple import reorder_suite from unittest import TestCase return reorder_suite(suite, (TestCase,))
def geodjango_suite(apps=True): """ Returns a TestSuite consisting only of GeoDjango tests that can be run. """ import sys from django.db.models import get_app suite = unittest.TestSuite() # Adding the GEOS tests. from django.contrib.gis.geos import tests as geos_tests suite.addTest(geos_tests.suite()) # Adding GDAL tests, and any test suite that depends on GDAL, to the # suite if GDAL is available. from django.contrib.gis.gdal import HAS_GDAL if HAS_GDAL: from django.contrib.gis.gdal import tests as gdal_tests suite.addTest(gdal_tests.suite()) else: sys.stderr.write('GDAL not available - no tests requiring GDAL will be run.\n') # Add GeoIP tests to the suite, if the library and data is available. from django.contrib.gis.geoip import HAS_GEOIP if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'): from django.contrib.gis.geoip import tests as geoip_tests suite.addTest(geoip_tests.suite()) # Finally, adding the suites for each of the GeoDjango test apps. if apps: for app_name in geo_apps(namespace=False): suite.addTest(build_suite(get_app(app_name))) return suite
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ 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) result = JUnitXMLTestRunner('.').run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def get_tests(self, test_labels=None): from django.test import simple simple.TEST_MODULE = self.test_module suite = self.testdb.FixtureTestSuiteFactory() 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)) return reorder_suite(suite, (TestCase,))
def build_suite(self, suite, **kwargs): if self.test_labels: for label in self.test_labels: if label not in self.exclude_apps: if '.' in label: if self._check_not_excluded(label): suite.addTest(build_test(label)) else: try: app = get_app(label) if self._check_not_excluded(app.__name__): suite.addTest(build_suite(app)) except ImproperlyConfigured: pass else: for app in get_apps(): if self._check_not_excluded(app.__name__): suite.addTest(build_suite(app))
def test_suite_override(self): """ Validate that you can define a custom suite when running tests with ``django.test.simple.DjangoTestSuiteRunner`` (which builds up a test suite using ``build_suite``). """ app = get_app("test_suite_override") suite = build_suite(app) self.assertEqual(suite.countTestCases(), 1)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ Run the unit tests for all the test labels in the provided list. !!! This test runner work with REAL database, not with test!!! !!! When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ 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 #connection.creation.create_test_db(verbosity, autoclobber=not interactive) result = unittest.TextTestRunner(verbosity=old_name).run(suite) #connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def _build_suite(self, test_labels, extra_tests=None, **kwargs): 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(): app_name = app.__name__ if app_name.endswith('.models'): app_name = app_name[:-7] if app_name not in getattr(settings, 'TEST_SKIP_APP_TESTS', ()): suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase, ))
def build_suite(self, test_labels, extra_tests=None, **kwargs): 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(): if app.__name__.startswith('django.'): continue # disable django built-in tests suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase, ))
def build_tests(self): """ Build tests for inclusion in suite from resolved packages. TODO: Cleanup/simplify this method, flow too complex, too much duplication. """ from django.db.models import get_app from django.test.simple import build_suite tests = [] app = get_app(self.options['label'][0]) tests.append(build_suite(app)) return tests
def geodjango_suite(apps=True): """ Returns a TestSuite consisting only of GeoDjango tests that can be run. """ import sys from django.db.models import get_app suite = unittest.TestSuite() # Adding the GEOS tests. from django.contrib.gis.geos import tests as geos_tests suite.addTest(geos_tests.suite()) # Adding the measurment tests. from django.contrib.gis.tests import test_measure suite.addTest(test_measure.suite()) # Adding GDAL tests, and any test suite that depends on GDAL, to the # suite if GDAL is available. from django.contrib.gis.gdal import HAS_GDAL if HAS_GDAL: from django.contrib.gis.gdal import tests as gdal_tests suite.addTest(gdal_tests.suite()) from django.contrib.gis.tests import test_spatialrefsys, test_geoforms suite.addTest(test_spatialrefsys.suite()) suite.addTest(test_geoforms.suite()) else: sys.stderr.write( 'GDAL not available - no tests requiring GDAL will be run.\n') # Add GeoIP tests to the suite, if the library and data is available. from django.contrib.gis.utils import HAS_GEOIP if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'): from django.contrib.gis.tests import test_geoip suite.addTest(test_geoip.suite()) # Finally, adding the suites for each of the GeoDjango test apps. if apps: for app_name in geo_apps(namespace=False): suite.addTest(build_suite(get_app(app_name))) return suite
def build_tests(self): """ Build tests for inclusion in suite from resolved packages. """ from django.core.exceptions import ImproperlyConfigured from django.db.models import get_app from django.test.simple import build_suite tests = [] for package in self.packages: try: app_name = package.rsplit('.')[-1] app = get_app(app_name, emptyOK=True) tests.append(build_suite(app)) except ImproperlyConfigured, e: raise log.info("Warning: %s" % e) except ImportError, e: raise log.info("Warning: %s" % e)
def build_suite(self, test_labels, extra_tests=None, **kwargs): """ This method is overridden to construct a suite consisting only of tests for GeoDjango. """ suite = unittest.TestSuite() # Adding the GEOS tests. from django.contrib.gis.geos import tests as geos_tests suite.addTest(geos_tests.suite()) # Adding the measurment tests. from django.contrib.gis.tests import test_measure suite.addTest(test_measure.suite()) # Adding GDAL tests, and any test suite that depends on GDAL, to the # suite if GDAL is available. from django.contrib.gis.gdal import HAS_GDAL if HAS_GDAL: from django.contrib.gis.gdal import tests as gdal_tests suite.addTest(gdal_tests.suite()) from django.contrib.gis.tests import test_spatialrefsys, test_geoforms suite.addTest(test_spatialrefsys.suite()) suite.addTest(test_geoforms.suite()) else: sys.stderr.write( 'GDAL not available - no tests requiring GDAL will be run.\n') # Add GeoIP tests to the suite, if the library and data is available. from django.contrib.gis.utils import HAS_GEOIP if HAS_GEOIP and hasattr(settings, 'GEOIP_PATH'): from django.contrib.gis.tests import test_geoip suite.addTest(test_geoip.suite()) # Finally, adding the suites for each of the GeoDjango test apps. for app_name in self.geo_apps: suite.addTest(build_suite(get_app(app_name))) return suite
def build_suite(self, test_labels, extra_tests=None, **kwargs): suite = None pattern = '*_test.py' # Do default test loading if no test labels specfied if not test_labels: suite = unittest.TestSuite() for app in get_apps(): suite.addTest(build_suite(app)) # Then intelligent test find from top directory suite.addTest( unittest.defaultTestLoader.discover('.', pattern=pattern)) # If 'projectapps' in test_labels then find only the tests in my # project, but find all of them if 'projectapps' in test_labels: suite = unittest.TestSuite() suite.addTest( unittest.defaultTestLoader.discover('.', pattern=pattern)) # Else can only handle a single project name or a series of tests elif test_labels: root = '.' # Loads tests from dotted tests names suite = unittest.defaultTestLoader.loadTestsFromNames(test_labels) # if single named module has no tests, do discovery within it if not suite.countTestCases() and len(test_labels) == 1: suite = None root = import_module(test_labels[0]).__path__[0] suite = unittest.defaultTestLoader.discover(root, pattern=pattern) # Default DjangoTestSuiteRunner behavior if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(suite, (TestCase, ))
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], include_coverage=False, include_profile=False, profile_all=False): """ Copy and munge of django's django.test.simple.run_tests method, we're extending it to handle coverage -- Original Docs -- Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ utils.setup_test_environment() settings.DEBUG = False profile.PROFILE_ALL_TESTS = False suite = unittest.TestSuite() coverage_modules = [] if include_coverage: coverage.start() if profile_all: profile.PROFILE_ALL_TESTS = True if test_labels: for label in test_labels: if '.' in label: suite.addTest(simple.build_test(label)) else: app = models.get_app(label) suite.addTest(simple.build_suite(app)) coverage_modules.append(app) else: for app in models.get_apps(): if app.__name__.startswith('appengine_django'): continue suite.addTest(simple.build_suite(app)) coverage_modules.append(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) result = unittest.TextTestRunner(verbosity=verbosity).run(suite) if include_coverage: coverage.stop() app_names = [mod.__name__.split('.')[0] for mod in coverage_modules] coverage_paths = ['%s/*.py' % app for app in settings.INSTALLED_APPS if _any_startswith(app, app_names)] coverage.report(coverage_paths, ignore_errors=1) if profile_all or include_profile: f = open(settings.PROFILING_DATA_PATH, 'w') f.write(profile.csv()) f.close() profile.clear() connection.creation.destroy_test_db(old_name, verbosity) utils.teardown_test_environment() return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None): """ This module allows users to run tests for GIS apps that require the creation of a spatial database. Currently, this is only required for PostgreSQL as PostGIS needs extra overhead in test database creation. In order to create a PostGIS database, the DATABASE_USER (or TEST_DATABASE_USER, if defined) will require superuser priviliges. To accomplish this outside the `postgres` user, you have a few options: (A) Make your user a super user: This may be done at the time the user is created, for example: $ createuser --superuser <user_name> Or you may alter the user's role from the SQL shell (assuming this is done from an existing superuser role): postgres# ALTER ROLE <user_name> SUPERUSER; (B) Create your own PostgreSQL database as a local user: 1. Initialize database: `initdb -D /path/to/user/db` 2. If there's already a Postgres instance on the machine, it will need to use a different TCP port than 5432. Edit postgresql.conf (in /path/to/user/db) to change the database port (e.g. `port = 5433`). 3. Start this database `pg_ctl -D /path/to/user/db start` (C) On Windows platforms the pgAdmin III utility may also be used as a simple way to add superuser privileges to your database user. The TEST_RUNNER needs to be set in your settings like so: TEST_RUNNER='django.contrib.gis.tests.run_tests' Note: This test runner assumes that the PostGIS SQL files ('lwpostgis.sql' and 'spatial_ref_sys.sql') are installed in the directory specified by `pg_config --sharedir` (and defaults to /usr/local/share if that fails). This behavior is overridden if POSTGIS_SQL_PATH is set in your settings. Windows users should set POSTGIS_SQL_PATH manually because the output of `pg_config` uses paths like 'C:/PROGRA~1/POSTGR~1/..'. Finally, the tests may be run by invoking `./manage.py test`. """ from django.conf import settings from django.db import connection from django.db.models import get_app, get_apps from django.test.simple import build_suite, build_test from django.test.utils import setup_test_environment, teardown_test_environment # The `create_spatial_db` routine abstracts away all the steps needed # to properly construct a spatial database for the backend. from django.contrib.gis.db.backend import create_spatial_db # Setting up for testing. setup_test_environment() settings.DEBUG = False old_name = settings.DATABASE_NAME # The suite may be passed in manually, e.g., when we run the GeoDjango test, # we want to build it and pass it in due to some customizations. Otherwise, # the normal test suite creation process from `django.test.simple.run_tests` # is used to create the test suite. if suite is None: 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) # Creating the test spatial database. create_spatial_db(test=True, verbosity=verbosity) # Executing the tests (including the model tests), and destorying the # test database after the tests have completed. result = unittest.TextTestRunner(verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() # Returning the total failures and errors return len(result.failures) + len(result.errors)
def run_tests_with_coverage(test_labels, verbosity=1, interactive=True, extra_tests=[], xml_out=False): """ Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ setup_test_environment() settings.DEBUG = False suite = unittest.TestSuite() modules_to_cover = [] # start doing some coverage action cov = coverage.coverage() cov.erase() cov.start() # if passed a list of tests... 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)) # ...otherwise use all installed else: for app in get_apps(): # skip apps named "Django" because they use a database if not app.__name__.startswith('django'): # get the actual app name app_name = app.__name__.replace('.models', '') # get a list of the files inside that module files = glob('%s/*.py' % app_name) # remove models because we don't use them, stupid new_files = [i for i in files if not i.endswith('models.py')] modules_to_cover.extend(new_files) # actually test the file suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) result = unittest.TextTestRunner(verbosity=verbosity).run(suite) teardown_test_environment() # stop coverage cov.stop() # output results print '' print '--------------------------' print 'Unit test coverage results' print '--------------------------' print '' if xml_out: # using the same output directory as the --xml function uses for testing if not os.path.isdir(os.path.join("temp", "xml")): os.makedirs(os.path.join("temp", "xml")) output_filename = 'temp/xml/coverage_output.xml' cov.xml_report(morfs=coverage_modules, outfile=output_filename) cov.report(modules_to_cover, show_missing=1) return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ worsk exactly as per normal test but only creates the test_db if it doesn't yet exist and does not destroy it when done tables are flushed and fixtures loaded between tests as per usual but if your schema has not changed then this saves significant amounts of time and speeds up the test cycle Run the unit tests for all the test labels in the provided list. Labels must be of the form: - app.TestClass.test_method Run a single specific test method - app.TestClass Run all the test methods in a given class - app Search for doctests and unittests in the named application. When looking for tests, the test runner will look in the models and tests modules for the application. A list of 'extra' tests may also be provided; these tests will be added to the test suite. Returns the number of tests that failed. """ 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 = apps.get_app(label) suite.addTest(build_suite(app)) else: for app in apps.get_apps(): suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) suite = reorder_suite(suite, (TestCase, )) old_name = settings.DATABASES['default']['NAME'] # Everything up to here is from django.test.simple from django.db.backends import creation from django.db import connection, DatabaseError if settings.DATABASES['default']['TEST_NAME']: settings.DATABASES['default']['NAME'] = settings.DATABASES['default'][ 'TEST_NAME'] else: settings.DATABASES['default'][ 'NAME'] = creation.TEST_DATABASE_PREFIX + settings.DATABASES[ 'default']['NAME'] connection.settings_dict["DATABASE_NAME"] = settings.DATABASES['default'][ 'NAME'] # does test db exist already ? try: if settings.DATABASES['default']['ENGINE'] == 'sqlite3': if not os.path.exists(settings.DATABASES['default']['NAME']): raise DatabaseError cursor = connection.cursor() except Exception: # db does not exist # juggling ! create_test_db switches the DATABASE_NAME to the TEST_DATABASE_NAME settings.DATABASES['default']['NAME'] = old_name connection.settings_dict["DATABASE_NAME"] = old_name connection.creation.create_test_db(verbosity, autoclobber=True) else: connection.close() settings.DATABASES['default'][ 'SUPPORTS_TRANSACTIONS'] = connections_support_transactions() result = unittest.TextTestRunner(verbosity=verbosity).run(suite) # Since we don't call destory_test_db, we need to set the db name back. settings.DATABASES['default']['NAME'] = old_name connection.settings_dict["DATABASE_NAME"] = old_name teardown_test_environment() return len(result.failures) + len(result.errors)