Example #1
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)
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=[]):
    """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 #4
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 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)
Example #6
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
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 #9
0
 def build_suite(self, test_labels, extra_tests=None, **kwargs):
     full_suite = DjRunner.build_suite(self,
                                       None,
                                       extra_tests=None,
                                       **kwargs)
     my_suite = unittest.TestSuite()
     labels_for_suite = []
     if test_labels:
         full_re = []
         for label in test_labels:
             if re.findall(r'(^[\w\d_]+(?:\.[\w\d_]+)*$)',
                           label) == [label]:
                 labels_for_suite.append(label)
                 continue
             text_for_re = label.replace('.', '\.').replace('*', '[^\.]+?')
             if 'DjangoTestSuiteRunner' in self.mro_names:
                 if len(label.split('.')) > 2:
                     text_for_re += '$'
                 else:
                     text_for_re += '\..+$'
                 app = get_app(label.split('.')[0])
                 text_for_re = text_for_re.replace(
                     label.split('.')[0],
                     app.__name__.split('.models')[0])
             elif 'DiscoverRunner' in self.mro_names:
                 if len(label.split('.')) > 3:
                     text_for_re += '$'
                 else:
                     text_for_re += '\..+$'
             full_re.append(text_for_re)
         full_re = '(^' + ')|(^'.join(full_re) + ')' if full_re else ''
         if 'DjangoTestSuiteRunner' in self.mro_names:
             apps = [app.__name__.split('.models')[0] for app in get_apps()]
         for el in full_suite._tests:
             module_name = el.__module__
             if 'DjangoTestSuiteRunner' in self.mro_names:
                 while module_name and module_name not in apps:
                     module_name = '.'.join(module_name.split('.')[:-1])
             full_name = [
                 module_name, el.__class__.__name__, el._testMethodName
             ]
             full_name = '.'.join(full_name)
             if (full_re and re.findall(r'%s' % full_re, full_name)):
                 my_suite.addTest(el)
     if labels_for_suite:
         my_suite.addTests(
             DjRunner.build_suite(self,
                                  labels_for_suite,
                                  extra_tests=None,
                                  **kwargs))
     return reorder_suite(my_suite, (unittest.TestCase, ))
Example #10
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 #11
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 #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 = 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():
            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 #14
0
 def build_suite(self, test_labels, extra_tests=None, **kwargs):
     full_suite = DjRunner.build_suite(self, None, extra_tests=None, **kwargs)
     my_suite = unittest.TestSuite()
     labels_for_suite = []
     if test_labels:
         full_re = []
         for label in test_labels:
             if re.findall(r'(^[\w\d_]+(?:\.[\w\d_]+)*$)', label) == [label]:
                 labels_for_suite.append(label)
                 continue
             text_for_re = label.replace('.', '\.').replace('*', '[^\.]+?')
             if 'DjangoTestSuiteRunner' in self.mro_names:
                 if len(label.split('.')) > 2:
                     text_for_re += '$'
                 else:
                     text_for_re += '\..+$'
                 app = get_app(label.split('.')[0])
                 text_for_re = text_for_re.replace(label.split('.')[0], app.__name__.split('.models')[0])
             elif 'DiscoverRunner' in self.mro_names:
                 if len(label.split('.')) > 3:
                     text_for_re += '$'
                 else:
                     text_for_re += '\..+$'
             full_re.append(text_for_re)
         full_re = '(^' + ')|(^'.join(full_re) + ')' if full_re else ''
         if 'DjangoTestSuiteRunner' in self.mro_names:
             apps = [app.__name__.split('.models')[0] for app in get_apps()]
         for el in full_suite._tests:
             module_name = el.__module__
             if 'DjangoTestSuiteRunner' in self.mro_names:
                 while module_name and module_name not in apps:
                     module_name = '.'.join(module_name.split('.')[:-1])
             full_name = [module_name, el.__class__.__name__, el._testMethodName]
             full_name = '.'.join(full_name)
             if (full_re and re.findall(r'%s' % full_re, full_name)):
                 my_suite.addTest(el)
     if labels_for_suite:
         my_suite.addTests(DjRunner.build_suite(self, labels_for_suite, extra_tests=None, **kwargs))
     return reorder_suite(my_suite, (unittest.TestCase,))
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_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)