def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None): """ Set `TEST_RUNNER` in your settings with this routine in order to scaffold test spatial databases correctly for your GeoDjango models. For more documentation, please consult the following URL: http://geodjango.org/docs/testing.html. """ from django.conf import settings from django.db import connection from django.db.models import get_app, get_apps from django.test.simple import build_suite, build_test, reorder_suite, TestCase from django.test.utils import setup_test_environment, teardown_test_environment # The `create_test_spatial_db` routine abstracts away all the steps needed # to properly construct a spatial database for the backend. from django.contrib.gis.db.backend import create_test_spatial_db # Setting up for testing. setup_test_environment() settings.DEBUG = False old_name = settings.DATABASE_NAME # Creating the test spatial database. create_test_spatial_db(verbosity=verbosity, autoclobber=not interactive) # The suite may be passed in manually, e.g., when we run the GeoDjango test, # we want to build it and pass it in due to some customizations. Otherwise, # the normal test suite creation process from `django.test.simple.run_tests` # is used to create the test suite. if suite is None: suite = unittest.TestSuite() if test_labels: for label in test_labels: if '.' in label: suite.addTest(build_test(label)) else: app = get_app(label) suite.addTest(build_suite(app)) else: for app in get_apps(): suite.addTest(build_suite(app)) for test in extra_tests: suite.addTest(test) suite = reorder_suite(suite, (TestCase, )) # Executing the tests (including the model tests), and destorying the # test database after the tests have completed. result = unittest.TextTestRunner(verbosity=verbosity).run(suite) connection.creation.destroy_test_db(old_name, verbosity) teardown_test_environment() # Returning the total failures and errors return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=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(test_labels, verbosity=1, interactive=True, spatial_db=False): """Test runner that invokes nose.""" # Prepare django for testing. utils.setup_test_environment() old_db_name = settings.DATABASE_NAME if spatial_db: from django.contrib.gis.db.backend import create_test_spatial_db create_test_spatial_db(verbosity, autoclobber=not interactive) else: connection.creation.create_test_db(verbosity, autoclobber=not interactive) # Pretend it's a production environment. settings.DEBUG = False # We pass nose a list of arguments that looks like sys.argv, but customize # to avoid unknown django arguments. nose_argv = ['nosetests'] if hasattr(settings, 'NOSE_ARGS'): nose_argv.extend(settings.NOSE_ARGS) # Skip over 'manage.py test' and any arguments handled by django. django_opts = ['--noinput'] for opt in BaseCommand.option_list: django_opts.extend(opt._long_opts) django_opts.extend(opt._short_opts) nose_argv.extend(opt for opt in sys.argv[2:] if not any(opt.startswith(d) for d in django_opts)) if verbosity >= 1: print ' '.join(nose_argv) try: result_plugin = ResultPlugin() test_program = nose.core.TestProgram(argv=nose_argv, exit=False, addplugins=[result_plugin]) result = result_plugin.result return len(result.failures) + len(result.errors) finally: # Clean up django. connection.creation.destroy_test_db(old_db_name, verbosity) utils.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. """ import os, os.path, shutil 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 settings.TESTING = True old_name = settings.DATABASE_NAME settings.CACHE_DIR = os.path.join(os.path.dirname(__file__), 'cache') if not os.path.exists(settings.CACHE_DIR): os.makedirs(settings.CACHE_DIR) settings.FEED_PATH = os.path.join(settings.CACHE_DIR, 'feeds') settings.EXTERNAL_IMAGE_DIR = os.path.join(settings.CACHE_DIR, 'external_images') settings.GENERATED_MAP_DIR = os.path.join(settings.CACHE_DIR, 'generated_maps') settings.OSM_TILE_DIR = os.path.join(settings.CACHE_DIR, 'osm_tiles') output = getattr(settings, 'TEST_OUTPUT_FILE', 'test_results.xml') output = open(output, 'w') # Creating the test spatial database. create_test_spatial_db(verbosity=0, 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 = xmlrunner.XMLTestRunner(stream=output).run(suite) connection.creation.destroy_test_db(old_name, verbosity=0) teardown_test_environment() # Remove the cache directories shutil.rmtree(settings.CACHE_DIR) # Returning the total failures and errors return len(result.failures) + len(result.errors)
def run_tests(test_labels, verbosity=1, interactive=True, extra_tests=[], suite=None): """ This module allows users to run tests for GIS apps that require the creation of a spatial database. Currently, this is only required for PostgreSQL as PostGIS needs extra overhead in test database creation. In order to create a PostGIS database, the DATABASE_USER (or TEST_DATABASE_USER, if defined) will require superuser priviliges. To accomplish this outside the `postgres` user, you have a few options: (A) Make your user a super user: This may be done at the time the user is created, for example: $ createuser --superuser <user_name> Or you may alter the user's role from the SQL shell (assuming this is done from an existing superuser role): postgres# ALTER ROLE <user_name> SUPERUSER; (B) Create your own PostgreSQL database as a local user: 1. Initialize database: `initdb -D /path/to/user/db` 2. If there's already a Postgres instance on the machine, it will need to use a different TCP port than 5432. Edit postgresql.conf (in /path/to/user/db) to change the database port (e.g. `port = 5433`). 3. Start this database `pg_ctl -D /path/to/user/db start` (C) On Windows platforms the pgAdmin III utility may also be used as a simple way to add superuser privileges to your database user. The TEST_RUNNER needs to be set in your settings like so: TEST_RUNNER='django.contrib.gis.tests.run_tests' Note: This test runner assumes that the PostGIS SQL files ('lwpostgis.sql' and 'spatial_ref_sys.sql') are installed in the directory specified by `pg_config --sharedir` (and defaults to /usr/local/share if that fails). This behavior is overridden if POSTGIS_SQL_PATH is set in your settings. Windows users should set POSTGIS_SQL_PATH manually because the output of `pg_config` uses paths like 'C:/PROGRA~1/POSTGR~1/..'. Finally, the tests may be run by invoking `./manage.py test`. """ from django.conf import settings from django.db import connection from django.db.models import get_app, get_apps from django.test.simple import build_suite, build_test from django.test.utils import setup_test_environment, teardown_test_environment # The `create_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) # 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) # 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)