Example #1
0
    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
Example #2
0
  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
Example #3
0
    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 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, ))
Example #5
0
    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 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)
Example #7
0
    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, ))
Example #8
0
    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,))
Example #9
0
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)
Example #10
0
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):
        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,))
Example #12
0
    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,))
Example #13
0
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
Example #14
0
def testing_an_individual_test_via_managepy():
    from tests.app import tests

    assert tests.TEST_HAS_RUN == False
    test_suite = build_test("app.test_case.test_change_global")
    test_suite.debug()  # runs the tests
    assert tests.TEST_HAS_RUN == True
Example #15
0
    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,))
Example #16
0
    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
Example #17
0
    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
Example #18
0
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)
Example #19
0
	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)
Example #20
0
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=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_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)
Example #24
0
    def build_suite(self, test_labels, *args, **kwargs):
        """
        Override the default test suite builder to exclude doctests, use
        'tests.selenium' as the test module path, allow excluding the tests of
        any apps contained in settings.SELENIUM['EXCLUDE_APPS'].

        """
        from django.test.simple import (unittest, build_test, get_app,
                get_apps, reorder_suite, TestCase, doctest,
                build_suite as _build_suite)

        # Hack to remove doctests from test suite without reimplementing
        # build_suite
        def _filter_suite(suite):
            tests = []
            for test in suite._tests:
                if isinstance(test, unittest.TestSuite):
                    tests.append(_filter_suite(test))
                elif not isinstance(test, doctest.DocTestCase):
                    tests.append(test)

            suite._tests = tests
            return suite

        def build_suite(*args, **kwargs):
            suite = _build_suite(*args, **kwargs)
            return _filter_suite(suite)

        exclude_apps = settings.SELENIUM_SETUP.get('EXCLUDE_APPS', [])
        test_labels = [l for l in test_labels
                       if all(not l.startswith(app) for app in exclude_apps)]

        import django.test.simple
        orig_test_module = django.test.simple.TEST_MODULE
        django.test.simple.TEST_MODULE = SELENIUM_TEST_MODULE

        try:
            # copied from django TestSuiteRunner
            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():
                    name = app.__name__
                    if all(('.%s' % a) not in name for a in exclude_apps):
                        suite.addTest(build_suite(app))

            return reorder_suite(suite, (TestCase,))

        finally:
            django.test.simple.TEST_MODULE = orig_test_module
Example #25
0
    def test_discover_within_package(self):
        """
        Verify labels like applabel.TestCase find tests defined in
        applabel/tests/__init__.py. Fixes #22478.
        """

        from django.test.simple import build_test
        suite = build_test('valid_app.SampleTest')
        self.assertEqual(suite.countTestCases(), 1)
Example #26
0
    def test_discover_within_package(self):
        """
        Verify labels like applabel.TestCase find tests defined in
        applabel/tests/__init__.py. Fixes #22478.
        """

        from django.test.simple import build_test
        suite = build_test('valid_app.SampleTest')
        self.assertEqual(suite.countTestCases(), 1)
Example #27
0
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
Example #28
0
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)
Example #29
0
 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))
Example #30
0
 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 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.
        TODO: Cleanup/simplify this method, flow too complex,
        too much duplication.
        """
        from django.core.exceptions import ImproperlyConfigured
        from django.db.models import get_app
        from django.test.simple import build_suite, build_test

        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), 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), e:
                                    self.handle_label_exception(e)
                            else:
                                app = get_app(package)
                                tests.append(build_suite(app))
                            break
                        except LabelException:
                            raise
Example #33
0
 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))
Example #34
0
    def build_suite(self, test_labels, *args, **kwargs):
        """
        Override the default test suite builder to exclude doctests, use
        'tests.selenium' as the test module path.

        """
        from django.test.simple import (unittest, build_test, get_app,
                get_apps, reorder_suite, TestCase, doctest,
                build_suite as _build_suite)

        # Hack to remove doctests from test suite without reimplementing
        # build_suite
        def _filter_suite(suite):
            tests = []
            for test in suite._tests:
                if isinstance(test, unittest.TestSuite):
                    tests.append(_filter_suite(test))
                elif not isinstance(test, doctest.DocTestCase):
                    tests.append(test)

            suite._tests = tests
            return suite

        def build_suite(*args, **kwargs):
            suite = _build_suite(*args, **kwargs)
            return _filter_suite(suite)

        import django.test.simple
        orig_test_module = django.test.simple.TEST_MODULE
        django.test.simple.TEST_MODULE = SELENIUM_TEST_MODULE

        try:
            # copied from django TestSuiteRunner
            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))

            return reorder_suite(suite, (TestCase,))

        finally:
            django.test.simple.TEST_MODULE = orig_test_module
Example #35
0
 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))
Example #36
0
def run_tests_until_fail(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.

    Stops the tests at the first failure and returns 1.  If all test pass,
    returns 0.

    Also displays only the first failure in the failing test suite.
    """
    setup_test_environment()

    settings.DEBUG = False
    suite = 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)
    result = FailStopTextTestRunner(verbosity=verbosity).run(suite)
    destroy_test_db(old_name, verbosity)

    teardown_test_environment()

    return len(result.failures) + len(result.errors)
Example #37
0
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)
Example #38
0
def optimize_apps_for_test_labels(test_labels):
    test_map = AppAndTestMap()
    for label in test_labels:
        if "." in label:
            test_map.add_test(label.split(".")[0], build_test(label))
        else:
            test_map.add_app(label)

    _real_installed_apps = settings.INSTALLED_APPS
    needed_apps = test_map.get_needed_installed_apps()
    print "overriding settings.INSTALLED_APPS to {}".format(",".join(test_map.get_needed_installed_apps()))
    settings.INSTALLED_APPS = tuple(needed_apps)
    apps.set_installed_apps(settings.INSTALLED_APPS)
    try:
        yield
    finally:
        settings.INSTALLED_APPS = _real_installed_apps
        apps.unset_installed_apps()
 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))
Example #40
0
    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,))
Example #41
0
    def build_suite(self, test_labels, extra_tests=None, **kwargs):
        suite = TestCaseSuite()

        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))

        if extra_tests:
            for test in extra_tests:
                suite.addTest(test)

        return suite
Example #42
0
    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, self.test_type, self.host))
        else:
            for app in get_apps():
                suite.addTest(build_suite(app, self.test_type, self.host))

        if extra_tests:
            for test in extra_tests:
                suite.addTest(test)

        return reorder_suite(suite, (unittest.TestCase,))
Example #43
0
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, ))
Example #44
0
    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, ))
Example #45
0
def optimize_apps_for_test_labels(test_labels):
    # TODO make this work on Django 1.8 (django.test.simple has been removed)
    from django.test.simple import build_test
    test_map = AppAndTestMap()
    for label in test_labels:
        if isinstance(label, tuple):
            test_map.add_test(*label)
        elif '.' in label:
            test_map.add_test(label.split('.')[0], build_test(label))
        else:
            test_map.add_app(label)

    _real_installed_apps = settings.INSTALLED_APPS
    needed_apps = test_map.get_needed_installed_apps()
    print 'overriding settings.INSTALLED_APPS to {}'.format(','.join(
        test_map.get_needed_installed_apps()))
    settings.INSTALLED_APPS = tuple(needed_apps)
    apps.set_installed_apps(settings.INSTALLED_APPS)
    try:
        yield
    finally:
        settings.INSTALLED_APPS = _real_installed_apps
        apps.unset_installed_apps()
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)
Example #48
0
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)
Example #49
0
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)