From 5d5e20d481e8b14fe4a96f8f774840dc2a94c6a9 Mon Sep 17 00:00:00 2001 From: Michael Mendy Date: Sun, 11 Feb 2024 10:25:45 -0800 Subject: [PATCH] Separates the configuration of settings into its function `configure_settings()` Separates the configuration of settings into its function `configure_settings()` --- testrunner.py | 49 ++++++++++++++++++++++++------------------------- 1 file changed, 24 insertions(+), 25 deletions(-) diff --git a/testrunner.py b/testrunner.py index 00c367f..cf4e10a 100755 --- a/testrunner.py +++ b/testrunner.py @@ -1,36 +1,34 @@ import os import sys - -from django.conf import settings - import django +from django.conf import settings -def runtests(): - test_dir = os.path.dirname(os.path.abspath(__file__)) - sys.path.insert(0, test_dir) +def configure_settings(): + """Configure Django settings for the test environment.""" + base_dir = os.path.dirname(os.path.abspath(__file__)) + sys.path.insert(0, base_dir) settings.configure( DEBUG=True, - DATABASES={ - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - } - }, - INSTALLED_APPS=('django.contrib.auth', - 'django.contrib.contenttypes', - 'django.contrib.sessions', - 'django.contrib.admin', - 'django.contrib.messages', - 'experiments',), + DATABASES={'default': {'ENGINE': 'django.db.backends.sqlite3'}}, + INSTALLED_APPS=( + 'django.contrib.auth', + 'django.contrib.contenttypes', + 'django.contrib.sessions', + 'django.contrib.admin', + 'django.contrib.messages', + 'experiments', # Your app to test + ), ROOT_URLCONF='experiments.tests.urls', - MIDDLEWARE = ( + MIDDLEWARE=( 'django.contrib.sessions.middleware.SessionMiddleware', + 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', - 'django.contrib.messages.middleware.MessageMiddleware' + 'django.contrib.messages.middleware.MessageMiddleware', ), SECRET_KEY="foobarbaz", - TEMPLATES = [ + TEMPLATES=[ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], @@ -44,15 +42,16 @@ def runtests(): }, ], ) - django.setup() - +def run_tests(): + """Run the Django tests for the specified apps.""" + django.setup() # Initialize Django from django.test.utils import get_runner TestRunner = get_runner(settings) test_runner = TestRunner(verbosity=1, failfast=False) - failures = test_runner.run_tests(['experiments', ]) + failures = test_runner.run_tests(['experiments']) # Specify your app names here sys.exit(bool(failures)) - if __name__ == '__main__': - runtests() + configure_settings() + run_tests()