def runtests(options):
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
django.setup()
parent = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, parent)
from django.test.runner import DiscoverRunner
runner_class = DiscoverRunner
test_args = ["dach.tests"]
failures = runner_class(verbosity=options.verbosity,
interactive=options.interactive,
failfast=options.failfast).run_tests(test_args)
sys.exit(failures)
python类setup()的实例源码
def run_tests(*test_args):
if not test_args:
test_args = ['tests']
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(test_args)
sys.exit(bool(failures))
def setup(app):
"""Sphinx extension: run sphinx-apidoc."""
event = 'builder-inited' if six.PY3 else b'builder-inited'
app.connect(event, on_init)
def runtests():
test_dir = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, test_dir)
settings.configure(
DEBUG=True,
SECRET_KEY='123',
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3'
}
},
INSTALLED_APPS=[
'django.contrib.auth',
'django.contrib.contenttypes',
'drf_simple_auth'
],
ROOT_URLCONF='drf_simple_auth.urls',
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
},
]
)
django.setup()
from django.test.utils import get_runner
TestRunner = get_runner(settings) # noqa
test_runner = TestRunner(verbosity=1, interactive=True)
if hasattr(django, 'setup'):
django.setup()
failures = test_runner.run_tests(['drf_simple_auth'])
sys.exit(bool(failures))
def pytest_configure():
settings.configure(
STATIC_URL="/static/"
)
django.setup()
def run_tests(self):
# import here, cause outside the eggs aren't loaded
import django
from django.conf import settings
from django.test.utils import get_runner
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(['tests'])
sys.exit(bool(failures))
def main():
# configure django settings with test settings
from tests import settings as test_settings
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(['tests'])
sys.exit(failures)
def pytest_configure():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'pytests.django_settings')
import django
django.setup()
from django.core.management import execute_from_command_line
execute_from_command_line(['manage.py', 'makemigrations', 'test_app'])
execute_from_command_line(['manage.py', 'migrate'])
def runtests():
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
test_runner = get_runner(settings)
if sys.argv[0] != 'setup.py' and len(sys.argv) > 1:
tests = sys.argv[1:]
else:
tests = ['tests']
failures = test_runner().run_tests(tests)
sys.exit(bool(failures))
def run_tests(*test_args):
if not test_args:
test_args = ['tests']
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(test_args)
sys.exit(bool(failures))
def runtests(*test_args):
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.test_settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(["tests"])
sys.exit(bool(failures))
def get_wsgi_application():
"""
The public interface to Django's WSGI support. Should return a WSGI
callable.
Allows us to avoid making django.core.handlers.WSGIHandler public API, in
case the internal WSGI implementation changes or moves in the future.
"""
django.setup()
return WSGIHandler()
def run_tests(*test_args):
if not test_args:
test_args = ['tests']
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings'
django.setup()
TestRunner = get_runner(settings)
test_runner = TestRunner()
failures = test_runner.run_tests(test_args)
sys.exit(bool(failures))
def get_wsgi_application():
"""
The public interface to Django's WSGI support. Should return a WSGI
callable.
Allows us to avoid making django.core.handlers.WSGIHandler public API, in
case the internal WSGI implementation changes or moves in the future.
"""
django.setup(set_prefix=False)
return WSGIHandler()
def setup():
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings')
import django
django.setup()
def prepare(name):
setup()
from django.core.cache import caches
obj = caches[name]
for key in range(RANGE):
key = str(key).encode('utf-8')
obj.set(key, key)
try:
obj.close()
except:
pass
def pytest_configure(config):
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tests.settings')
if config.getoption('postgres'):
os.environ['DATABASE_ENGINE'] = 'django.db.backends.postgresql_psycopg2'
os.environ['DATABASE_NAME'] = 'oscar_wagtail'
django.setup()
def init_django():
# Making Django run this way is a two-step process. First, call
# settings.configure() to give Django settings to work with:
from django.conf import settings
settings.configure(**SETTINGS_DICT)
# Then, call django.setup() to initialize the application cache
# and other bits:
import django
django.setup()
# Originally we defined this as a session-scoped fixture, but that
# broke django.test.SimpleTestCase instances' class setup methods,
# so we need to call this function *really* early.
def setup(self):
envs = self.conf.get('env', ())
if not envs:
return
assert isinstance(envs, dict), "Environments must be an object holding keys and values"
transportLogger.info("Settings up environment variables")
for env, value in envs.items():
transportLogger.debug('ENV: %s : %s' % (str(env), str(value)))
os.environ[env] = value
def setup_django(cls, settings_module='', project_path=''):
try:
import django
except ImportError:
raise TransportRequirementError('Unable to import %s' % cls._type.title())
os.environ['DJANGO_SETTINGS_MODULE'] = settings_module
sys.path.append(project_path)
try:
django.setup()
except django.core.exceptions.ImproperlyConfigured:
raise TransportLoadError('Django setup failed')
django.db.connections.close_all()