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 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 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 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=[]): """ 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 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_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)
def tearDownModule(): if not django: return # remove all the stuff that django installed if teardown_test_environment: teardown_test_environment() if destroy_test_db: destroy_test_db(context['DB_NAME'], verbosity=0) if storage: rmtree(storage.location, ignore_errors=True) sys.path = context['sys.path'] util.replace_dict(context['sys.modules'], sys.modules) util.replace_dict(context['os.environ'], os.environ)
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 create_test_db(verbosity, autoclobber=not interactive) result = unittest.TextTestRunner(verbosity=verbosity).run(suite) destroy_test_db(old_name, verbosity) teardown_test_environment() return len(result.failures) + len(result.errors)
def run_tests(module_list, verbosity=1, interactive=True): """ Run the tests that require creation of a spatial database. In order to run geographic model tests the DATABASE_USER will require superuser priviliges. To accomplish this outside the `postgres` user, create your own PostgreSQL database as a 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` On Windows platforms simply use the pgAdmin III utility to add superuser privileges to your database user. Make sure your settings.py matches the settings of the user database. For example, set the same port number (`DATABASE_PORT=5433`). DATABASE_NAME or TEST_DATABSE_NAME must be set, along with DATABASE_USER. In settings.py set TEST_RUNNER='django.contrib.gis.tests.run_tests'. Finally, this 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 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.contrib.gis.db.backend import create_spatial_db from django.db import connection from django.test.utils import destroy_test_db # Getting initial values. old_debug = settings.DEBUG old_name = copy(settings.DATABASE_NAME) old_installed = copy(settings.INSTALLED_APPS) new_installed = copy(settings.INSTALLED_APPS) # Want DEBUG to be set to False. settings.DEBUG = False from django.db.models import loading # Creating the test suite, adding the test models to INSTALLED_APPS, and # adding the model test suites to our suite package. test_suite = suite() for test_model in test_models: module_name = 'django.contrib.gis.tests.%s' % test_model if mysql: test_module_name = 'tests_mysql' else: test_module_name = 'tests' new_installed.append(module_name) # Getting the test suite tsuite = getattr(__import__('django.contrib.gis.tests.%s' % test_model, globals(), locals(), [test_module_name]), test_module_name) test_suite.addTest(tsuite.suite()) # Resetting the loaded flag to take into account what we appended to # the INSTALLED_APPS (since this routine is invoked through # django/core/management, it caches the apps; this ensures that syncdb # will see our appended models) settings.INSTALLED_APPS = new_installed loading.cache.loaded = False # Creating the test spatial database. create_spatial_db(test=True, verbosity=verbosity) # Executing the tests (including the model tests) result = TextTestRunner(verbosity=verbosity).run(test_suite) # Cleaning up, destroying the test spatial database and resetting the INSTALLED_APPS. destroy_test_db(old_name, verbosity) settings.DEBUG = old_debug settings.INSTALLED_APPS = old_installed # Returning the total failures and errors return len(result.failures) + len(result.errors)
def run_tests(module_list, verbosity=1, interactive=True): """ Run the tests that require creation of a spatial database. In order to run geographic model tests the DATABASE_USER will require superuser priviliges. To accomplish this outside the `postgres` user, create your own PostgreSQL database as a 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` On Windows platforms simply use the pgAdmin III utility to add superuser privileges to your database user. Make sure your settings.py matches the settings of the user database. For example, set the same port number (`DATABASE_PORT=5433`). DATABASE_NAME or TEST_DATABSE_NAME must be set, along with DATABASE_USER. In settings.py set TEST_RUNNER='django.contrib.gis.tests.run_tests'. Finally, this 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 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.contrib.gis.db.backend import create_spatial_db from django.db import connection from django.test.utils import destroy_test_db # Getting initial values. old_debug = settings.DEBUG old_name = copy(settings.DATABASE_NAME) old_installed = copy(settings.INSTALLED_APPS) new_installed = copy(settings.INSTALLED_APPS) # Want DEBUG to be set to False. settings.DEBUG = False from django.db.models import loading # Creating the test suite, adding the test models to INSTALLED_APPS, and # adding the model test suites to our suite package. test_suite = suite() for test_model in test_models: module_name = 'django.contrib.gis.tests.%s' % test_model if mysql: test_module_name = 'tests_mysql' else: test_module_name = 'tests' new_installed.append(module_name) # Getting the test suite tsuite = getattr( __import__('django.contrib.gis.tests.%s' % test_model, globals(), locals(), [test_module_name]), test_module_name) test_suite.addTest(tsuite.suite()) # Resetting the loaded flag to take into account what we appended to # the INSTALLED_APPS (since this routine is invoked through # django/core/management, it caches the apps; this ensures that syncdb # will see our appended models) settings.INSTALLED_APPS = new_installed loading.cache.loaded = False # Creating the test spatial database. create_spatial_db(test=True, verbosity=verbosity) # Executing the tests (including the model tests) result = TextTestRunner(verbosity=verbosity).run(test_suite) # Cleaning up, destroying the test spatial database and resetting the INSTALLED_APPS. destroy_test_db(old_name, verbosity) settings.DEBUG = old_debug settings.INSTALLED_APPS = old_installed # Returning the total failures and errors return len(result.failures) + len(result.errors)