def split_suite(self, suite): """ Check if any of the tests to run subclasses TransactionTestCase. """ simple_tests = unittest.TestSuite() db_tests = unittest.TestSuite() for test in suite: if isinstance(test, TransactionTestCase): db_tests.addTest(test) else: simple_tests.addTest(test) return simple_tests, db_tests
def suite(): """ This function is called automatically by the django test runner. This also collates tests from packages that are not formally django applications. """ from src.locks import tests as locktests from src.utils import tests as utiltests from src.commands.default import tests as commandtests tsuite = unittest.TestSuite() tsuite.addTest( unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])) # test modules from non-django apps tsuite.addTest( unittest.defaultTestLoader.loadTestsFromModule(commandtests)) tsuite.addTest(unittest.defaultTestLoader.loadTestsFromModule(locktests)) tsuite.addTest(unittest.defaultTestLoader.loadTestsFromModule(utiltests)) for path in glob.glob("../src/tests/test_*.py"): testmod = mod_import(path) tsuite.addTest(unittest.defaultTestLoader.loadTestsFromModule(testmod)) #from src.tests import test_commands_cmdhandler #tsuite.addTest(unittest.defaultTestLoader.loadTestsFromModule(test_commands_cmdhandler)) return tsuite
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: 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 suite(): tsuite = unittest.TestSuite() tsuite.addTest( unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])) return tsuite
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 suite(): #testcases = [PledgeTest, AuthorizeTest, TransactionTest] testcases = [TransactionTest, CreditTest, AccountTest] suites = unittest.TestSuite([unittest.TestLoader().loadTestsFromTestCase(testcase) for testcase in testcases]) return suites
def load_tests(loader, tests, pattern): suite = unittest.TestSuite() suite.addTests(tests) suite.addTest(DocTestSuite(util)) suite.addTest(DocTestSuite(oath)) return suite
def load_test_suite(package_name): suite = unittest.TestSuite() loader = unittest.TestLoader() for test in loader.discover(package_name): suite.addTest(test) return lambda: suite
def suite(): simple = unittest.TestLoader().loadTestsFromTestCase(SimpleNav) data = unittest.TestLoader().loadTestsFromTestCase(DataNav) uact = unittest.TestLoader().loadTestsFromTestCase(UserActions) suite = unittest.TestSuite( [ uact, simple, data ]) return suite
def suite(): """ This function is called automatically by the django test runner. """ tsuite = unittest.TestSuite() tsuite.addTest( unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])) tsuite.addTest(unittest.defaultTestLoader.loadTestsFromModule(vamdctests)) return tsuite
def suite(): """ Define a suite that deliberately ignores a test defined in this module. """ testSuite = unittest.TestSuite() testSuite.addTest(SampleTests('testGoodStuff')) return testSuite
def suite(): """ This function is called automatically by the django test runner. This also runs the command tests defined in src/commands/default/tests.py. """ tsuite = unittest.TestSuite() tsuite.addTest(unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])) tsuite.addTest(unittest.defaultTestLoader.loadTestsFromModule(vamdctests)) return tsuite
def suite(): suite = unittest.TestSuite() suite.addTest(unittest.TestLoader().loadTestsFromTestCase( test_twitter_views.TwitterViewsTestCase)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase( test_twitter_api.TwitterApiTestCase)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase( test_views.ViewsTestCase)) suite.addTest(unittest.TestLoader().loadTestsFromTestCase( test_scheduler.SchedulerTestCase)) return suite
def suite(): suite = unittest.TestSuite() suite.addTest(RequestTestCase('test_edit_notification_settings')) suite.addTest(RequestTestCase('test_import_ratings')) suite.addTest(RequestTestCase('test_password_change')) suite.addTest(RequestTestCase('test_change_display_name')) suite.addTest(AvatarTestCase('test_avatar_caching')) suite.addTest(AvatarTestCase('test_no_avatar_in_storage')) return suite
def suite(): """ Generate the test suite. """ s = unittest.TestSuite() s.addTest(doctest.DocTestSuite(rdflib_django)) s.addTest(doctest.DocTestSuite(store)) s.addTest(unittest.findTestCases(test_store)) s.addTest(unittest.findTestCases(test_rdflib)) s.addTest(unittest.findTestCases(test_seq)) s.addTest(unittest.findTestCases(test_namespaces)) return s
def build_suite(self, test_labels=None, extra_tests=None, **kwargs): # Make django-unaccent searchable unaccent_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src', 'django_unaccent') sys.path.insert(0, unaccent_dir) suite = unittest.TestSuite() from django_unaccent import tests suite.addTest(tests.suite()) 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 build_suite(self, *args, **kwargs): """ Check if any of the tests to run subclasses TransactionTestCase. """ suite = super(TwoStageTestRunner, self).build_suite(*args, **kwargs) simple_tests = unittest.TestSuite() db_tests = TimingTestSuite() for test in suite: if isinstance(test, TransactionTestCase): db_tests.addTest(test) else: simple_tests.addTest(test) return simple_tests, db_tests
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 build_suite(self, test_labels, extra_tests=None, **kwargs): suite = unittest.TestSuite() for label in test_labels: if '.' in label: print("Ignoring label with dot in: %s" % label) continue app = get_app(label) features_dir = get_features(app) if features_dir is not None: suite.addTest(self.make_bdd_test_suite(features_dir)) return reorder_suite(suite, (unittest.TestCase, ))
def build_suite(app_module): """ Create a complete Django test suite for the provided application module. """ suite = unittest.TestSuite() # Load unit and doctests in the models.py module. If module has # a suite() method, use it. Otherwise build the test suite ourselves. if hasattr(app_module, 'suite'): test = app_module.suite() if len(test._tests) > 0: suite.addTests(test._tests) else: test = unittest.TestLoader().loadTestsFromModule(app_module) if len(test._tests) > 0: suite.addTests(test._tests) try: test = doctest.DocTestSuite(app_module, checker=doctestOutputChecker, runner=DocTestRunner) if len(test._tests) > 0: suite.addTests(test._tests) except ValueError: # No doc tests in models.py pass # Check to see if a separate 'tests' module exists parallel to the # models module test_module = get_tests(app_module) if test_module: # Load unit and doctests in the tests.py module. If module has # a suite() method, use it. Otherwise build the test suite ourselves. if hasattr(test_module, 'suite'): test = test_module.suite() if len(test._tests) > 0: suite.addTests(test._tests) else: test = unittest.TestLoader().loadTestsFromModule(test_module) if len(test._tests) > 0: suite.addTests(test._tests) try: test = doctest.DocTestSuite(test_module, checker=doctestOutputChecker, runner=DocTestRunner) if len(test._tests) > 0: suite.addTests(test._tests) except ValueError: # No doc tests in tests.py pass return suite
def reorder_suite(suite, classes): """ Reorders a test suite by test type. `classes` is a sequence of types All tests of type classes[0] are placed first, then tests of type classes[1], etc. Tests with no match in classes are placed last. """ class_count = len(classes) bins = [unittest.TestSuite() for i in range(class_count + 1)] partition_suite(suite, classes, bins) for i in range(class_count): bins[0].addTests(bins[i + 1]) return bins[0]
def suite(): loader = unittest.TestLoader() s = unittest.TestSuite() for ver in ( "1.0", "1.1", ): # for ver in ("1.1", ): module = import_module('film20.api.api_%s.tests' % ver.replace(".", "_")) s.addTests(loader.loadTestsFromModule(module)) s.addTests(loader.loadTestsFromModule(test_oauth)) return s
def filter_by_tag(self, tests): """ Filter test suite to only include tagged tests. """ def tags_in(test, tags=[]): return [tag in getattr(test.__class__, 'tags', []) for tag in tags] suite = unittest.TestSuite() for test in tests: if hasattr(test, '__iter__'): suite.addTest(self.filter_by_tag(test)) else: if any(tags_in(test, self.tags)) or not self.tags: if not any(tags_in(test, self.exclude_tags)): suite.addTest(test) return suite
def _filter_suite(self, suite): filters = getattr(settings, 'TEST_RUNNER_FILTER', None) if filters is None: # We do NOT filter if filters are not set return suite filtered = unittest.TestSuite() for test in suite: if isinstance(test, unittest.TestSuite): filtered.addTests(self._filter_suite(test)) else: for f in filters: if test.id().startswith(f): filtered.addTest(test) return filtered
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(): suite.addTest(build_suite(app)) if extra_tests: for test in extra_tests: suite.addTest(test) return reorder_suite(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_config = apps.get_app_config(label) suite.addTest(build_suite(app_config)) else: for app_config in apps.get_app_configs(): suite.addTest(build_suite(app_config)) if extra_tests: for test in extra_tests: suite.addTest(test) return runner.reorder_suite(suite, (unittest.TestCase, ))
def test_failfast(self): class MockTestOne(unittest.TestCase): def runTest(self): assert False class MockTestTwo(unittest.TestCase): def runTest(self): assert False suite = unittest.TestSuite([MockTestOne(), MockTestTwo()]) mock_stream = StringIO.StringIO() dtr = simple.DjangoTestRunner(verbosity=0, failfast=False, stream=mock_stream) result = dtr.run(suite) self.assertEqual(2, result.testsRun) self.assertEqual(2, len(result.failures)) dtr = simple.DjangoTestRunner(verbosity=0, failfast=True, stream=mock_stream) result = dtr.run(suite) self.assertEqual(1, result.testsRun) self.assertEqual(1, len(result.failures))