def tearDownModule(): if django is None: # pragma: no cover return runner = test_state['runner'] runner_state = test_state['runner_state'] runner.teardown_databases(runner_state) django_test_utils.teardown_test_environment()
def tearDown(self): super().tearDown() self.env_patch.stop() self.exclude_patch.stop() self.traced_patch.stop() teardown_test_environment() _django_instrumentor.uninstrument()
def after_testfile(self): # Those imports must be done **after** setup_environ was called from django.test.utils import teardown_test_environment from django.test.utils import destroy_test_db teardown_test_environment() print('destroying', self.dbname) destroy_test_db(self.dbname, verbosity=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
def run_tests(module_list, verbosity=1, extra_tests=[]): """ Run the unit tests for all the modules in the provided list. This testrunner will search each of the modules in the provided list, looking for doctests and unittests in models.py or tests.py within the module. 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() for module in module_list: suite.addTest(build_suite(module)) for test in extra_tests: suite.addTest(test) old_name = settings.DATABASE_NAME create_test_db(verbosity) result = unittest.TextTestRunner(verbosity=verbosity).run(suite) destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def finalize(self, result=None): """ Clean up any created database and schema. """ if not self.settings_path: # short circuit if no settings file can be found return from django.test.utils import teardown_test_environment from django.db import connection from django.conf import settings self.call_plugins_method('beforeDestroyTestDb', settings, connection) try: connection.creation.destroy_test_db( self.old_db, verbosity=self.verbosity, ) except Exception: # If we can't tear down the test DB, don't worry about it. pass self.call_plugins_method('afterDestroyTestDb', settings, connection) self.call_plugins_method( 'beforeTeardownTestEnv', settings, teardown_test_environment) teardown_test_environment() self.call_plugins_method('afterTeardownTestEnv', settings)
def run_tests(module_list, verbosity=1, extra_tests=[]): """ Run the unit tests for all the modules in the provided list. This testrunner will search each of the modules in the provided list, looking for doctests and unittests in models.py or tests.py within the module. 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() for module in module_list: suite.addTest(build_suite(module)) for test in extra_tests: suite.addTest(test) old_name = settings.DATABASE_NAME create_test_db(verbosity) result = unittest.TextTestRunner(verbosity=verbosity).run(suite) destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures)
def pytest_unconfigure(config): dbsettings = settings.DATABASES['default'] if django.VERSION >= (1, 7): dbtestname = dbsettings['TEST']['NAME'] else: dbtestname = dbsettings['TEST_NAME'] connection.close() if dbsettings['ENGINE'].split('.')[-1] == 'postgresql_psycopg2': connection.connection = None connection.settings_dict['NAME'] = dbtestname.split('_')[1] cursor = connection.cursor() connection.autocommit = True if django.VERSION < (1, 6): connection._set_isolation_level(0) else: connection._set_autocommit(True) time.sleep(1) sys.stdout.write("Destroying test database for alias '%s' (%s)...\n" % (connection.alias, dbtestname)) sys.stdout.flush() cursor.execute('DROP DATABASE %s' % connection.ops.quote_name(dbtestname)) else: connection.creation.destroy_test_db(dbtestname, verbosity=2) teardown_test_environment()
def tearDown(self): from django.test.utils import teardown_test_environment from opencensus.trace import execution_context execution_context.clear() teardown_test_environment()
def pytest_unconfigure(config): dbsettings = settings.DATABASES['default'] if django.VERSION >= (1, 7): dbtestname = dbsettings['TEST']['NAME'] else: dbtestname = dbsettings['TEST_NAME'] connection.close() if dbsettings['ENGINE'].split('.')[-1] == 'postgresql_psycopg2': connection.connection = None connection.settings_dict['NAME'] = dbtestname.split('_')[1] cursor = connection.cursor() connection.autocommit = True if django.VERSION < (1, 6): connection._set_isolation_level(0) else: connection._set_autocommit(True) time.sleep(1) sys.stdout.write( "Destroying test database for alias '%s' (%s)...\n" % ( connection.alias, dbtestname) ) sys.stdout.flush() cursor.execute( 'DROP DATABASE %s' % connection.ops.quote_name(dbtestname)) else: connection.creation.destroy_test_db(dbtestname, verbosity=2) teardown_test_environment()
def after_testfile(self): # Those imports must be done **after** setup_environ was called from django.test.utils import teardown_test_environment from django.test.utils import destroy_test_db teardown_test_environment() print 'destroying', self.dbname destroy_test_db(self.dbname, verbosity=0)
def run_tests(test_labels=APPS, verbosity=1, interactive=True, extra_tests=[]): """Hack on top of django.test.simple.run_tests to use the XML test runner provided by Bitten""" setup_test_environment() settings.DEBUG = False suite = unittest.TestSuite() if test_labels: for label in test_labels: if "." in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: for app in get_apps(): suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) suite = reorder_suite(suite, (TestCase,)) old_name = settings.DATABASE_NAME from django.db import connection connection.creation.create_test_db(verbosity, autoclobber=not interactive) result = XMLTestRunner(stream=sys.stdout, xml_stream=open("test-results.xml", "w")).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() return result
def run_tests(verbosity=1, interactive=False): from django.conf import settings from django.core import management from django.db import connection from django.test.utils import setup_test_environment, \ teardown_test_environment setup_test_environment() settings.DEBUG = False if not os.path.exists(settings.EXTENSIONS_MEDIA_ROOT): os.mkdir(settings.EXTENSIONS_MEDIA_ROOT, 0755) old_db_name = 'default' connection.creation.create_test_db(verbosity, autoclobber=not interactive) management.call_command('syncdb', verbosity=verbosity, interactive=interactive) nose_argv = ['runtests.py', '-v', '--with-coverage', '--with-doctest', '--doctest-extension=.txt', '--cover-package=djblets'] if len(sys.argv) > 2: node_argv += sys.argv[2:] nose.main(argv=nose_argv) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment()
def inner_run(self, *test_labels, **options): """Inside the re-running test processes, this is a thread""" todo = self.config.get('todo', test_labels) if '*' in todo: todo = [] if not todo: self.app.coverage_start() setup_test_environment() test_runner = get_test_runner(**options) try: suite = test_runner.build_suite(todo, None) except ImportError: print "Error, selected test module can't be found: %s" % str(todo) return self.ask_rerun() except AttributeError: print "Error, selected test function can't be found: %s" % str(todo) return self.ask_rerun() result = test_runner.run_suite(suite) teardown_test_environment() if not todo: self.app.coverage_report() failures = set() for test, _ in result.errors + result.failures: (name, module) = str(test).rsplit(')', 1)[0].split(' (') if module == 'unittest.loader.ModuleImportFailure': (module, name) = name.rsplit('.', 1) failures.add('%s.%s' % (module, name)) failures = list(failures) if not failures: if test_labels != todo: set_title('NOW PASS!') sys.stdout.write("\nFinally working!\n\n") if self.config.get('select', []): # Set todo back to original selection self.config['todo'] = self.config['select'] else: # Clear error todo (reset to test_labels) del self.config['todo'] self.save_config() else: set_title('PASS') print "\nStill working!\n" return self.ask_rerun() # Add all failues to next todo list (for re-run) self.config['todo'] = failures self.save_config() print_failures(failures) return self.ask_rerun(failures)
def test_01_full(self): self._fixture_teardown() teardown_test_environment() call_command('test', 'localcrawler') call_command('localcrawler') setup_test_environment() self._fixture_setup()
def finalize(self, result=None): """ Clean up any created database and schema. """ if not self.settings_path: # short circuit if no settings file can be found return from django.test.utils import teardown_test_environment from django.db import connection from django.conf import settings from django.core.urlresolvers import clear_url_caches self.call_plugins_method('beforeDestroyTestDb', settings, connection) connection.creation.destroy_test_db(self.old_db, verbosity=self.verbosity) self.call_plugins_method('afterDestroyTestDb', settings, connection) self.call_plugins_method( 'beforeTeardownTestEnv', settings, teardown_test_environment) teardown_test_environment() self.call_plugins_method('afterTeardownTestEnv', settings) if hasattr(self, 'old_urlconf'): settings.ROOT_URLCONF = self.old_urlconf clear_url_caches()
def finalize(self, result=None): from django.test.utils import teardown_test_environment teardown_test_environment() # Tearing down the database: if self.create_db: from django.db import connection connection.creation.destroy_test_db(self.db_name, self.verbosity)
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(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 tear_down(): # destroy test database from django.db import connection connection.creation.destroy_test_db("not_needed") # teardown environment from django.test.utils import teardown_test_environment teardown_test_environment()
def post_test(self, conf): self.stop_app_threat() # flush database with django execute_from_command_line(['scriptname', 'flush', '--noinput']) dutils.teardown_databases(conf, 1) dutils.teardown_test_environment() subprocess.run(['/sbin/iptables', '-F', 'INPUT']) subprocess.run(['/sbin/iptables', '-F', 'OUTPUT'])
def run_tests(): os.environ['DJANGO_SETTINGS_MODULE'] = 'ptest.settings' setup_test_environment() connection.creation.create_test_db(verbosity=0, autoclobber=True) try: yield finally: teardown_test_environment()
def tearDownModule(): if django is None: return global DJANGO_RUNNER global DJANGO_RUNNER_STATE if DJANGO_RUNNER and DJANGO_RUNNER_STATE: DJANGO_RUNNER.teardown_databases(DJANGO_RUNNER_STATE) django_test_utils.teardown_test_environment()
def tearDownModule(): runner = test_state["runner"] runner_state = test_state["runner_state"] runner.teardown_databases(runner_state) utils.teardown_test_environment() shutil.rmtree(settings.MEDIA_ROOT, ignore_errors=True)
def run_tests(verbosity=1, interactive=False): from django.conf import settings from django.core import management from django.db import connections from django.test.utils import setup_test_environment, \ teardown_test_environment if hasattr(django, 'setup'): # Django >= 1.7 django.setup() setup_test_environment() settings.DEBUG = False old_db_names = [] for alias in connections: connection = connections[alias] old_db_names.append((connection, connection.settings_dict['NAME'])) connection.creation.create_test_db(verbosity, autoclobber=not interactive) if django.VERSION[:2] >= (1, 7): management.call_command('migrate', verbosity=verbosity, interactive=interactive) else: management.call_command('syncdb', verbosity=verbosity, interactive=interactive) nose_argv = [ 'runtests.py', '-v', '--with-coverage', '--with-doctest', '--doctest-extension=.txt', '--cover-package=django_evolution', '--match=tests[\/]*.py', '--match=^test', '--exclude-dir=django_evolution/tests/db', ] if len(sys.argv) > 2: nose_argv += sys.argv[2:] result = nose.main(argv=nose_argv, exit=False) for connection, name in old_db_names: connection.creation.destroy_test_db(name, verbosity=0) teardown_test_environment() if result.success: return 0 else: return 1
def inner_run(self, *test_labels, **options): todo = self.config.get('todo', test_labels) if '*' in todo: todo = [] if not todo: self.app.coverage_start() setup_test_environment() test_runner = self.TestRunner(**options) try: suite = test_runner.build_suite(todo, None) except ImportError: print "Error, selected test module can't be found: %s" % str(todo) return self.ask_rerun() except AttributeError: print "Error, selected test function can't be found: %s" % str(todo) return self.ask_rerun() result = test_runner.run_suite(suite) teardown_test_environment() if not todo: self.app.coverage_report() failures = [] for test, err in result.errors + result.failures: (name, module) = str(test).rsplit(')', 1)[0].split(' (') if module == 'unittest.loader.ModuleImportFailure': (module, name) = name.rsplit('.', 1) failures.append('%s.%s' % (module, name)) if not failures: if test_labels != todo: self.set_title('NOW PASS!') print "\nFinally working!\n" # Clear error todo (reset to test_labels) del self.config['todo'] self.save_config() else: self.set_title('PASS') print "\nStill working!\n" return self.ask_rerun() # Add all failues to next todo list (for re-run) self.config['todo'] = failures self.save_config() self.set_title('FAIL [%d]' % len(failures)) # Print options for user to select test target but # also set all failed tests as targets for x, test in enumerate(failures): print " %d. %s " % (x+1, test) return self.ask_rerun(failures)
def afterTest(self, test): """ Clean up any changes to the test database. """ # Restore transaction support on tests from django.conf import settings from django.db import connections, transaction from django.test.utils import setup_test_environment, teardown_test_environment use_transaction_isolation = self._should_use_transaction_isolation( test, settings) if self._should_rebuild_schema(test): for connection in connections.all(): connection.creation.destroy_test_db( self.old_db, verbosity=self.verbosity) teardown_test_environment() setup_test_environment() for connection in connections.all(): connection.creation.create_test_db(verbosity=self.verbosity) self.restore_transaction_support(transaction) transaction.commit() if transaction.is_managed(): transaction.leave_transaction_management() # If connection is not closed Postgres can go wild with # character encodings. for connection in connections.all(): connection.close() logger.debug("Running syncdb") self._num_syncdb_calls += 1 self._loaded_test_fixtures = [] return if use_transaction_isolation: self.restore_transaction_support(transaction) logger.debug("Rolling back") transaction.rollback() if transaction.is_managed(): transaction.leave_transaction_management() # If connection is not closed Postgres can go wild with # character encodings. for connection in connections.all(): connection.close() else: # Have to clear the db even if we're using django because django # doesn't properly flush the database after a test. It relies on # flushing before a test, so we want to avoid the case where a django # test doesn't flush and then a normal test runs, because it will # expect the db to already be flushed self._flush_db() self._loaded_test_fixtures = [] self.call_plugins_method('afterRollback', settings)
def teadDownModule(): # remove all the stuff that django installed teardown_test_environment() sys.path = context['sys.path'] util.replace_dict(context['sys.modules'], sys.modules) util.replace_dict(context['os.environ'], os.environ) destroy_test_db(settings.DATABASE_NAME, 2)
def finalize(self, result): """ At the end, tear down our testbed """ from django.test.utils import teardown_test_environment teardown_test_environment() if not self.persist_test_database: self.teardown_databases(self.old_config, verbosity=False)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ This is basically a copy of the default django run_tests method, except with the addition of the coverage report as documented at http://siddhi.blogspot.com/2007/04/code-coverage-for-your-django-code.html """ if hasattr(settings, 'TEST_DATABASE_ENGINE'): settings.DATABASE_ENGINE = settings.TEST_DATABASE_ENGINE setup_test_environment() settings.DEBUG = False suite = unittest.TestSuite() if test_labels: for label in test_labels: if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: for app in get_apps(): suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) old_name = settings.DATABASE_NAME create_test_db(verbosity, autoclobber=not interactive) coverage.start() print("Running tests ...") result = unittest.TextTestRunner(verbosity=verbosity).run(suite) print("Done running tests.") coverage.stop() if not os.path.exists(settings.COVERAGE_DIR): os.makedirs(settings.COVERAGE_DIR) modules = [] for module_string in settings.COVERAGE_MODULES: module = __import__(module_string, globals(), locals(), [""]) modules.append(module) f, s, m, mf = coverage.analysis(module) fp = file(os.path.join(settings.COVERAGE_DIR, module_string + ".html"), "wb") coverage_color.colorize_file(f, outstream=fp, not_covered=mf) fp.close() coverage.report(modules) coverage.erase() destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[]): """ 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, failfast=False, 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 afterTestRun(self, event): """Nose2 hook for the end of the test run""" from django.test.utils import teardown_test_environment if self.session.verbosity > 1: # remove the testing-specific handler handler = logging.StreamHandler() self.logger.removeHandler(handler) self.dtsr.teardown_databases(self.old_config) teardown_test_environment()
def run_tests(test_labels, verbosity = 1, interactive = True, extra_tests=[]): """ This is basically a copy of the default django run_tests method, except with the addition of the coverage report as documented at http://siddhi.blogspot.com/2007/04/code-coverage-for-your-django-code.html """ if hasattr(settings, 'TEST_DATABASE_ENGINE'): settings.DATABASE_ENGINE = settings.TEST_DATABASE_ENGINE setup_test_environment() settings.DEBUG = False suite = unittest.TestSuite() if test_labels: for label in test_labels: if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: for app in get_apps(): suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) old_name = settings.DATABASE_NAME create_test_db(verbosity, autoclobber=not interactive) coverage.start() print "Running tests ..." result = unittest.TextTestRunner(verbosity=verbosity).run(suite) print "Done running tests." coverage.stop() if not os.path.exists(settings.COVERAGE_DIR): os.makedirs(settings.COVERAGE_DIR) modules = [] for module_string in settings.COVERAGE_MODULES: module = __import__(module_string, globals(), locals(), [""]) modules.append(module) f,s,m,mf = coverage.analysis(module) fp = file(os.path.join(settings.COVERAGE_DIR, module_string + ".html"), "wb") coverage_color.colorize_file(f, outstream=fp, not_covered=mf) fp.close() coverage.report(modules) coverage.erase() destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def run(request): """ Runs all the tests. """ # Some stuff. verbosity=1 interactive=True extra_tests=[] # Setup the response. response = HttpResponse() # Get the installed apps for this project. installed_apps = settings.INSTALLED_APPS # The list of applications which will be tested. test_labels = [] # Populate the test_labels list with the POST data. for item in request.POST.getlist('apps'): test_labels.append(settings.INSTALLED_APPS[int(item)]) try: setup_test_environment() settings.DEBUG = False settings.DATABASE_ENGINE = 'sqlite3' suite = unittest.TestSuite() if test_labels: for label in test_labels: if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: for app in get_apps(): suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) suite = reorder_suite(suite, (TestCase,)) old_name = settings.DATABASE_NAME from django.db import connection connection.creation.create_test_db(verbosity, autoclobber=not interactive) result = HTMLTestRunner.HTMLTestRunner(stream=response, verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() except Exception, e: from ipdb import set_trace; set_trace() raise e
def test_runner( suite, verbosity=0 ): "Runs the functional tests on a test database" from django.db import connection old_name = settings.DATABASE_NAME utils.setup_test_environment() connection.creation.create_test_db(verbosity=verbosity, autoclobber=True) result = unittest.TextTestRunner(verbosity=2).run(suite) connection.creation.destroy_test_db(old_name, verbosity) utils.teardown_test_environment()
def testIndex(self): try: teardown_test_environment() except AttributeError: pass setup_test_environment() response = self.client.get('') self.assertEqual(response.status_code, 200) self.assertContains(response.context['posts'], Post.objects.filter(text='book'))
def test_runner(suite, verbosity=0): "Runs the functional tests on a test database" from django.db import connection old_name = settings.DATABASE_NAME utils.setup_test_environment() connection.creation.create_test_db(verbosity=verbosity, autoclobber=True) result = unittest.TextTestRunner(verbosity=2).run(suite) connection.creation.destroy_test_db(old_name, verbosity) utils.teardown_test_environment()
def finalize(self, result): """ Teardown the test environment and destroy the test database. """ from django.db import connection from django.test.utils import teardown_test_environment connection.creation.destroy_test_db(self.original_db_name, self.verbosity) teardown_test_environment()
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None): """ Set `TEST_RUNNER` in your settings with this routine in order to scaffold test spatial databases correctly for your GeoDjango models. For more documentation, please consult the following URL: http://geodjango.org/docs/testing.html. """ from django.conf import settings from django.db import connection from django.db.models import get_app, get_apps from django.test.simple import build_suite, build_test from django.test.utils import setup_test_environment, teardown_test_environment # The `create_test_spatial_db` routine abstracts away all the steps needed # to properly construct a spatial database for the backend. from django.contrib.gis.db.backend import create_test_spatial_db # Setting up for testing. setup_test_environment() settings.DEBUG = False old_name = settings.DATABASE_NAME # Creating the test spatial database. create_test_spatial_db(verbosity=verbosity, autoclobber=not interactive) # The suite may be passed in manually, e.g., when we run the GeoDjango test, # we want to build it and pass it in due to some customizations. Otherwise, # the normal test suite creation process from `django.test.simple.run_tests` # is used to create the test suite. if suite is None: suite = unittest.TestSuite() if test_labels: for label in test_labels: if "." in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: for app in get_apps(): suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) suite = reorder_suite(suite, (TestCase,)) # Executing the tests (including the model tests), and destorying the # test database after the tests have completed. result = unittest.TextTestRunner(verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() # Returning the total failures and errors return len(result.failures) + len(result.errors)
def run_tests_and_repeat(self, views, repeats=3): """ Run the CATMAID performance tests and return a list of results and a list of repeats for every run. """ from django.test.utils import setup_test_environment, \ teardown_test_environment setup_test_environment() # Make sure all python code is compiled to not include this timing compileall.compile_path(maxlevels=10) self.log("Made sure all Python modules in sys.path are compiled") # We need a cursor to talk to the database cursor = self.connection.cursor() # Create test database, based on an existing template database db_name = "test_%s" % self.template_db_name self.create_db(cursor, db_name, self.template_db_name, self.target_tablespace) # Store new database configuration self.connection.close() old_db_name = settings.DATABASES[self.connection.alias]["NAME"] settings.DATABASES[self.connection.alias]["NAME"] = db_name self.connection.settings_dict["NAME"] = db_name # Ensure a connection for the side effect of initializing the test # database and login. self.connection.ensure_connection() self.client.login(username=self.username, password=self.password) # Test all views self.log("Testing all %s views" % len(views)) results = [] repeat_results = [] for v in views: # Ideally the DB cluster would be stopped here, OS caches would be # dropped (http://linux-mm.org/Drop_Caches) and then the DB cluster # would be restarted. results.append(self.test(v)) for r in range(repeats): repeat_results.append(self.test(v)) teardown_test_environment() # Restore the original database name self.connection.close() settings.DATABASES[self.connection.alias]["NAME"] = old_db_name self.connection.settings_dict["NAME"] = old_db_name self.connection.ensure_connection() self.drop_db(self.connection.cursor(), db_name) return results, repeat_results
def run_tests_and_repeat(self, views, repeats=3): """ Run the CATMAID performance tests and return a list of results and a list of repeats for every run. """ from django.test.utils import setup_test_environment, \ teardown_test_environment setup_test_environment() # Make sure all python code is compiled to not include this timing compileall.compile_path(maxlevels=10) self.log("Made sure all Python modules in sys.path are compiled") # We need a cursor to talk to the database cursor = self.connection.cursor() # Create test database, based on an existing template database db_name = "test_%s" % self.template_db_name self.create_db(cursor, db_name, self.template_db_name, self.target_tablespace) # Store new database configuration self.connection.close() old_db_name = settings.DATABASES[self.connection.alias]["NAME"] settings.DATABASES[self.connection.alias]["NAME"] = db_name self.connection.settings_dict["NAME"] = db_name # Ensure a connection for the side effect of initializing the test # database and login. self.connection.ensure_connection() self.client.login(username=self.username, password=self.password) # Test all views self.log("Testing all %s views" % len(views)) results = [] repeat_results = [[] for i in range(repeats)] for v in views: # Ideally the DB cluster would be stopped here, OS caches would be # dropped (http://linux-mm.org/Drop_Caches) and then the DB cluster # would be restarted. results.append(self.test(v)) for r in range(repeats): repeat_results[r].append(self.test(v)) teardown_test_environment() # Restore the original database name self.connection.close() settings.DATABASES[self.connection.alias]["NAME"] = old_db_name self.connection.settings_dict["NAME"] = old_db_name self.connection.ensure_connection() self.drop_db(self.connection.cursor(), db_name) return results, repeat_results
def run_tests(): setup_test_environment() db_name = settings.DATABASE_NAME suite = unittest.TestSuite() for module_name in TEST_MODULES: module = import_module(module_name) suite.addTest(unittest.defaultTestLoader.loadTestsFromModule(module)) connection.creation.create_test_db() unittest.TextTestRunner().run(suite) connection.creation.destroy_test_db(db_name) teardown_test_environment()
def run(self): setup_test_environment() suite = unittest.TestLoader().loadTestsFromModule(functionaltest) if self.opts.xml: xmlrunner.XMLTestRunner(self.out).run(suite) if self.out: self.out.close() else: unittest.TextTestRunner(verbosity=2).run(suite) teardown_test_environment()
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 tearDownModule(): # remove all the stuff that django installed teardown_test_environment() sys.path = context['sys.path'] util.replace_dict(context['sys.modules'], sys.modules) util.replace_dict(context['os.environ'], os.environ) destroy_test_db(settings.DATABASE_NAME, verbosity=0) rmtree(storage.location, ignore_errors=True)
def run_tests(verbosity=1, interactive=False): from django.conf import settings from django.core import management from django.db import connection from django.test.utils import (setup_test_environment, teardown_test_environment) try: from django import setup # Django >= 1.7 setup() except ImportError: # Django < 1.7 pass # Restore warnings, if Django turns them off. warnings.simplefilter('default') setup_test_environment() settings.DEBUG = False for path in (settings.MEDIA_ROOT, settings.STATIC_ROOT): if not os.path.exists(path): os.mkdir(path, 0755) old_db_name = 'default' connection.creation.create_test_db(verbosity, autoclobber=not interactive) management.call_command('syncdb', verbosity=verbosity, interactive=interactive) nose_argv = [ 'runtests.py', '-v', '--with-coverage', '--with-doctest', '--doctest-extension=.txt', '--cover-package=djblets' ] if len(sys.argv) > 2: nose_argv += sys.argv[2:] # If the test files are executable on the file system, nose will need the # --exe argument to run them known_file = os.path.join(os.path.dirname(__file__), '..', 'djblets', 'settings.py') if (os.path.exists(known_file) and os.stat(known_file).st_mode & stat.S_IXUSR): nose_argv.append('--exe') nose.main(argv=nose_argv) connection.creation.destroy_test_db(old_db_name, verbosity) teardown_test_environment()
def run(self, max_depth=3): for p in self.plugins: p.set_output_dir(self.output_dir) old_DEBUG = settings.DEBUG settings.DEBUG = False setup_test_environment() test_signals.start_run.send(self) # To avoid tainting our memory usage stats with startup overhead we'll # do one extra request for the first page now: self.c.get(*self.not_crawled[0][-1]) while self.not_crawled: # Take top off not_crawled and evaluate it current_depth, from_url, to_url = self.not_crawled.pop(0) if current_depth > max_depth: continue try: transaction.enter_transaction_management() try: resp, returned_urls = self.get_url(from_url, to_url) except HTMLParser as e: LOG.error("%s: unable to parse invalid HTML: %s", to_url, e) except Exception as e: LOG.exception("%s had unhandled exception: %s", to_url, e) continue finally: transaction.rollback() except AttributeError: continue self.crawled[to_url] = True # Find its links that haven't been crawled for base_url in returned_urls: if not self.ascend and not base_url.startswith(self.base_url): LOG.debug("Skipping %s - outside scope of %s", base_url, self.base_url) continue if base_url not in [to for dep, fro, to in self.not_crawled ] and base_url not in self.crawled: self.not_crawled.append( (current_depth + 1, to_url, base_url)) test_signals.finish_run.send(self) teardown_test_environment() settings.DEBUG = old_DEBUG
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(): #run_tests(build_suite(app)) 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() report_results_for_suite(suite, result) return len(result.failures) + len(result.errors)
def runner(module_list, verbosity=1, interactive=True, extra_tests=[]): setup_test_environment() settings.DEBUG = False # Default to testing in a non-subdir install. settings.SITE_ROOT = "/" settings.MEDIA_URL = settings.SITE_ROOT + 'media/' settings.ADMIN_MEDIA_PREFIX = settings.MEDIA_URL + 'admin/' settings.RUNNING_TEST = True setup_media_dirs() old_name = settings.DATABASE_NAME connection.creation.create_test_db(verbosity, autoclobber=not interactive) management.call_command('syncdb', verbosity=verbosity, interactive=interactive) # Nose uses all local modules, which is really silly. These were getting # tested (and failing), so turn them off. exclusion = '|'.join( ['setup_test_environment', 'teardown_test_environment']) nose_argv = [ 'test.py', '-v', '--with-doctest', '--doctest-extension=.txt', '-e', exclusion ] if '--with-coverage' in sys.argv: nose_argv += ['--with-coverage', '--cover-package=reviewboard'] sys.argv.remove('--with-coverage') for package in settings.TEST_PACKAGES: nose_argv.append('--where=%s' % package) if '--with-webtests' in sys.argv: nose_argv.append('--where=webtests') sys.argv.remove('--with-webtests') # manage.py captures everything before "--" if len(sys.argv) > 2 and sys.argv.__contains__("--"): nose_argv += sys.argv[(sys.argv.index("--") + 1):] nose.main(argv=nose_argv, exit=False) destroy_media_dirs() connection.creation.destroy_test_db(old_name, verbosity=0) teardown_test_environment()