Skip to content

Instantly share code, notes, and snippets.

@danizen
Created February 14, 2019 03:44
Show Gist options
  • Save danizen/dcecf1070cbc78a95622174ed4d9ca61 to your computer and use it in GitHub Desktop.
Save danizen/dcecf1070cbc78a95622174ed4d9ca61 to your computer and use it in GitHub Desktop.
Run a shell to check Django tests
#!/usr/bin/env python
import argparse
import os
import sys
from django.core.management import call_command
from tests.runtests import setup, teardown
def django_testshell(verbosity, test_labels):
state = setup(verbosity, test_labels, 1)
call_command('migrate', '--run-syncdb')
call_command('shell')
teardown(state)
def parse_args(prog, args):
parser = argparse.ArgumentParser(
prog=prog,
description='Run a shell with the apps of the django test suite')
parser.add_argument(
'modules', nargs='*', metavar='module',
help='Optional path(s) to test modules; e.g. "i18n" or '
'"i18n.tests.TranslationTests.test_lazy_objects".',
)
parser.add_argument(
'-v', '--verbosity', default=1, type=int, choices=[0, 1, 2, 3],
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output',
)
parser.add_argument(
'--settings',
help='Python path to settings module, e.g. "myproject.settings". If '
'this isn\'t provided, either the DJANGO_SETTINGS_MODULE '
'environment variable or "test_sqlite" will be used.',
)
return parser.parse_args(args)
def main():
options = parse_args(sys.argv[0], sys.argv[1:])
if options.settings :
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
else:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_sqlite')
options.settings = os.environ['DJANGO_SETTINGS_MODULE']
rv = django_testshell(options.verbosity, options.modules)
if rv:
sys.exit(1)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment