2017-03-07 23:10:32 +08:00
|
|
|
#!/usr/bin/env python
|
2016-02-07 10:24:36 +08:00
|
|
|
import argparse
|
2015-06-05 00:35:18 +08:00
|
|
|
import atexit
|
2015-09-16 02:13:34 +08:00
|
|
|
import copy
|
2021-03-17 23:23:04 +08:00
|
|
|
import gc
|
2011-08-12 16:43:52 +08:00
|
|
|
import os
|
|
|
|
import shutil
|
2018-10-26 01:52:29 +08:00
|
|
|
import socket
|
2011-08-12 16:43:52 +08:00
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import tempfile
|
2013-06-14 22:02:30 +08:00
|
|
|
import warnings
|
2005-07-29 23:15:40 +08:00
|
|
|
|
2019-04-13 18:54:03 +08:00
|
|
|
try:
|
|
|
|
import django
|
|
|
|
except ImportError as e:
|
|
|
|
raise RuntimeError(
|
|
|
|
'Django module not found, reference tests/README.rst for instructions.'
|
|
|
|
) from e
|
|
|
|
else:
|
|
|
|
from django.apps import apps
|
|
|
|
from django.conf import settings
|
|
|
|
from django.db import connection, connections
|
|
|
|
from django.test import TestCase, TransactionTestCase
|
|
|
|
from django.test.runner import default_test_processes
|
|
|
|
from django.test.selenium import SeleniumTestCaseBase
|
2020-07-22 23:37:52 +08:00
|
|
|
from django.test.utils import NullTimeKeeper, TimeKeeper, get_runner
|
2020-05-04 18:07:45 +08:00
|
|
|
from django.utils.deprecation import (
|
2021-01-14 17:40:19 +08:00
|
|
|
RemovedInDjango41Warning, RemovedInDjango50Warning,
|
2020-05-04 18:07:45 +08:00
|
|
|
)
|
2019-04-13 18:54:03 +08:00
|
|
|
from django.utils.log import DEFAULT_LOGGING
|
2014-02-27 05:48:20 +08:00
|
|
|
|
2017-09-10 22:34:18 +08:00
|
|
|
try:
|
|
|
|
import MySQLdb
|
|
|
|
except ImportError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
# Ignore informational warnings from QuerySet.explain().
|
2018-04-25 00:06:04 +08:00
|
|
|
warnings.filterwarnings('ignore', r'\(1003, *', category=MySQLdb.Warning)
|
2017-09-10 22:34:18 +08:00
|
|
|
|
2015-10-29 01:24:00 +08:00
|
|
|
# Make deprecation warnings errors to ensure no usage of deprecated features.
|
2021-01-14 17:40:19 +08:00
|
|
|
warnings.simplefilter('error', RemovedInDjango50Warning)
|
2020-05-04 18:07:45 +08:00
|
|
|
warnings.simplefilter('error', RemovedInDjango41Warning)
|
2020-02-06 16:46:59 +08:00
|
|
|
# Make resource and runtime warning errors to ensure no usage of error prone
|
|
|
|
# patterns.
|
|
|
|
warnings.simplefilter("error", ResourceWarning)
|
2015-11-14 04:54:05 +08:00
|
|
|
warnings.simplefilter("error", RuntimeWarning)
|
2015-10-29 01:24:00 +08:00
|
|
|
# Ignore known warnings in test dependencies.
|
|
|
|
warnings.filterwarnings("ignore", "'U' mode is deprecated", DeprecationWarning, module='docutils.io')
|
2020-12-10 04:27:32 +08:00
|
|
|
# RemovedInDjango41Warning: Ignore MemcachedCache deprecation warning.
|
|
|
|
warnings.filterwarnings(
|
|
|
|
'ignore',
|
|
|
|
'MemcachedCache is deprecated',
|
|
|
|
category=RemovedInDjango41Warning,
|
|
|
|
)
|
2014-02-27 05:48:20 +08:00
|
|
|
|
2021-03-17 23:23:04 +08:00
|
|
|
# Reduce garbage collection frequency to improve performance. Since CPython
|
|
|
|
# uses refcounting, garbage collection only collects objects with cyclic
|
|
|
|
# references, which are a minority, so the garbage collection threshold can be
|
|
|
|
# larger than the default threshold of 700 allocations + deallocations without
|
|
|
|
# much increase in memory usage.
|
|
|
|
gc.set_threshold(100_000)
|
|
|
|
|
2017-01-20 21:01:02 +08:00
|
|
|
RUNTESTS_DIR = os.path.abspath(os.path.dirname(__file__))
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2014-12-19 04:54:02 +08:00
|
|
|
TEMPLATE_DIR = os.path.join(RUNTESTS_DIR, 'templates')
|
|
|
|
|
2015-02-22 01:56:36 +08:00
|
|
|
# Create a specific subdirectory for the duration of the test suite.
|
|
|
|
TMPDIR = tempfile.mkdtemp(prefix='django_')
|
|
|
|
# Set the TMPDIR environment variable in addition to tempfile.tempdir
|
|
|
|
# so that children processes inherit it.
|
|
|
|
tempfile.tempdir = os.environ['TMPDIR'] = TMPDIR
|
2005-07-30 06:35:54 +08:00
|
|
|
|
2016-12-29 23:27:49 +08:00
|
|
|
# Removing the temporary TMPDIR.
|
|
|
|
atexit.register(shutil.rmtree, TMPDIR)
|
2015-06-05 00:35:18 +08:00
|
|
|
|
|
|
|
|
2021-04-06 23:46:05 +08:00
|
|
|
# This is a dict mapping RUNTESTS_DIR subdirectory to subdirectories of that
|
|
|
|
# directory to skip when searching for test modules.
|
|
|
|
SUBDIRS_TO_SKIP = {
|
|
|
|
'': {'import_error_package', 'test_runner_apps'},
|
|
|
|
'gis_tests': {'data'},
|
|
|
|
}
|
2010-04-15 02:17:44 +08:00
|
|
|
|
2006-05-27 05:28:12 +08:00
|
|
|
ALWAYS_INSTALLED_APPS = [
|
|
|
|
'django.contrib.contenttypes',
|
2006-06-20 22:27:44 +08:00
|
|
|
'django.contrib.auth',
|
2007-08-15 19:25:22 +08:00
|
|
|
'django.contrib.sites',
|
2006-05-27 05:28:12 +08:00
|
|
|
'django.contrib.sessions',
|
2009-12-10 00:57:23 +08:00
|
|
|
'django.contrib.messages',
|
2014-01-25 05:43:00 +08:00
|
|
|
'django.contrib.admin.apps.SimpleAdminConfig',
|
2010-10-20 09:33:24 +08:00
|
|
|
'django.contrib.staticfiles',
|
2006-05-27 05:28:12 +08:00
|
|
|
]
|
|
|
|
|
2015-11-07 23:12:37 +08:00
|
|
|
ALWAYS_MIDDLEWARE = [
|
2014-04-20 20:58:29 +08:00
|
|
|
'django.contrib.sessions.middleware.SessionMiddleware',
|
|
|
|
'django.middleware.common.CommonMiddleware',
|
|
|
|
'django.middleware.csrf.CsrfViewMiddleware',
|
|
|
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
|
|
|
'django.contrib.messages.middleware.MessageMiddleware',
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2014-04-20 20:58:29 +08:00
|
|
|
|
2015-02-10 08:20:39 +08:00
|
|
|
# Need to add the associated contrib app to INSTALLED_APPS in some cases to
|
|
|
|
# avoid "RuntimeError: Model class X doesn't declare an explicit app_label
|
2015-12-25 02:28:32 +08:00
|
|
|
# and isn't in an application in INSTALLED_APPS."
|
2015-02-10 08:20:39 +08:00
|
|
|
CONTRIB_TESTS_TO_APPS = {
|
2020-08-28 17:13:05 +08:00
|
|
|
'deprecation': ['django.contrib.flatpages', 'django.contrib.redirects'],
|
|
|
|
'flatpages_tests': ['django.contrib.flatpages'],
|
|
|
|
'redirects_tests': ['django.contrib.redirects'],
|
2015-02-10 08:20:39 +08:00
|
|
|
}
|
|
|
|
|
2010-12-22 07:42:12 +08:00
|
|
|
|
2021-06-04 13:09:33 +08:00
|
|
|
def get_test_modules(gis_enabled):
|
2021-04-06 23:46:05 +08:00
|
|
|
"""
|
|
|
|
Scan the tests directory and yield the names of all test modules.
|
|
|
|
|
|
|
|
The yielded names have either one dotted part like "test_runner" or, in
|
|
|
|
the case of GIS tests, two dotted parts like "gis_tests.gdal_tests".
|
|
|
|
"""
|
|
|
|
discovery_dirs = ['']
|
2021-06-04 13:09:33 +08:00
|
|
|
if gis_enabled:
|
2015-04-05 00:09:46 +08:00
|
|
|
# GIS tests are in nested apps
|
2021-04-06 23:46:05 +08:00
|
|
|
discovery_dirs.append('gis_tests')
|
2017-05-04 22:14:35 +08:00
|
|
|
else:
|
2021-04-06 23:46:05 +08:00
|
|
|
SUBDIRS_TO_SKIP[''].add('gis_tests')
|
2013-07-09 21:58:56 +08:00
|
|
|
|
2021-04-06 23:46:05 +08:00
|
|
|
for dirname in discovery_dirs:
|
|
|
|
dirpath = os.path.join(RUNTESTS_DIR, dirname)
|
|
|
|
subdirs_to_skip = SUBDIRS_TO_SKIP[dirname]
|
2021-06-06 14:56:34 +08:00
|
|
|
with os.scandir(dirpath) as entries:
|
|
|
|
for f in entries:
|
|
|
|
if (
|
|
|
|
'.' in f.name or
|
|
|
|
os.path.basename(f.name) in subdirs_to_skip or
|
|
|
|
f.is_file() or
|
|
|
|
not os.path.exists(os.path.join(f.path, '__init__.py'))
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
test_module = f.name
|
|
|
|
if dirname:
|
|
|
|
test_module = dirname + '.' + test_module
|
|
|
|
yield test_module
|
2005-07-30 06:35:54 +08:00
|
|
|
|
2013-07-09 21:17:26 +08:00
|
|
|
|
2021-06-04 13:03:34 +08:00
|
|
|
def get_filtered_test_modules(start_at, start_after, gis_enabled, test_labels=None):
|
2021-04-21 19:02:58 +08:00
|
|
|
if test_labels is None:
|
|
|
|
test_labels = []
|
2021-04-19 17:01:48 +08:00
|
|
|
# Reduce each test label to just the top-level module part.
|
2017-05-04 22:14:35 +08:00
|
|
|
test_labels_set = set()
|
|
|
|
for label in test_labels:
|
2021-04-19 17:01:48 +08:00
|
|
|
test_module = label.split('.')[0]
|
|
|
|
test_labels_set.add(test_module)
|
2017-05-04 22:14:35 +08:00
|
|
|
|
2021-06-04 13:03:34 +08:00
|
|
|
# It would be nice to put this validation earlier but it must come after
|
|
|
|
# django.setup() so that connection.features.gis_enabled can be accessed.
|
|
|
|
if 'gis_tests' in test_labels_set and not gis_enabled:
|
|
|
|
print('Aborting: A GIS database backend is required to run gis_tests.')
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
def _module_match_label(module_name, label):
|
|
|
|
# Exact or ancestor match.
|
|
|
|
return module_name == label or module_name.startswith(label + '.')
|
2013-06-04 14:09:29 +08:00
|
|
|
|
2021-06-04 13:03:34 +08:00
|
|
|
start_label = start_at or start_after
|
|
|
|
for test_module in get_test_modules(gis_enabled):
|
|
|
|
if start_label:
|
|
|
|
if not _module_match_label(test_module, start_label):
|
|
|
|
continue
|
|
|
|
start_label = ''
|
|
|
|
if not start_at:
|
|
|
|
assert start_after
|
|
|
|
# Skip the current one before starting.
|
|
|
|
continue
|
|
|
|
# If the module (or an ancestor) was named on the command line, or
|
|
|
|
# no modules were named (i.e., run all), include the test module.
|
|
|
|
if not test_labels or any(
|
|
|
|
_module_match_label(test_module, label) for label in test_labels_set
|
|
|
|
):
|
|
|
|
yield test_module
|
|
|
|
|
|
|
|
|
|
|
|
def setup_collect_tests(start_at, start_after, test_labels=None):
|
2010-10-09 22:44:54 +08:00
|
|
|
state = {
|
|
|
|
'INSTALLED_APPS': settings.INSTALLED_APPS,
|
|
|
|
'ROOT_URLCONF': getattr(settings, "ROOT_URLCONF", ""),
|
2014-12-19 04:54:02 +08:00
|
|
|
'TEMPLATES': settings.TEMPLATES,
|
2010-10-09 22:44:54 +08:00
|
|
|
'LANGUAGE_CODE': settings.LANGUAGE_CODE,
|
2011-06-30 17:06:19 +08:00
|
|
|
'STATIC_URL': settings.STATIC_URL,
|
2011-08-12 16:43:52 +08:00
|
|
|
'STATIC_ROOT': settings.STATIC_ROOT,
|
2015-11-07 23:12:37 +08:00
|
|
|
'MIDDLEWARE': settings.MIDDLEWARE,
|
2010-10-09 22:44:54 +08:00
|
|
|
}
|
2006-12-15 14:06:52 +08:00
|
|
|
|
2007-02-10 12:01:19 +08:00
|
|
|
# Redirect some settings for the duration of these tests.
|
2006-08-27 21:59:47 +08:00
|
|
|
settings.INSTALLED_APPS = ALWAYS_INSTALLED_APPS
|
2006-09-02 17:34:40 +08:00
|
|
|
settings.ROOT_URLCONF = 'urls'
|
2021-02-06 01:41:49 +08:00
|
|
|
settings.STATIC_URL = 'static/'
|
2015-02-22 01:56:36 +08:00
|
|
|
settings.STATIC_ROOT = os.path.join(TMPDIR, 'static')
|
2014-12-19 04:54:02 +08:00
|
|
|
settings.TEMPLATES = [{
|
|
|
|
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
|
|
|
'DIRS': [TEMPLATE_DIR],
|
|
|
|
'APP_DIRS': True,
|
|
|
|
'OPTIONS': {
|
|
|
|
'context_processors': [
|
|
|
|
'django.template.context_processors.debug',
|
2015-01-13 05:31:44 +08:00
|
|
|
'django.template.context_processors.request',
|
|
|
|
'django.contrib.auth.context_processors.auth',
|
2014-12-19 04:54:02 +08:00
|
|
|
'django.contrib.messages.context_processors.messages',
|
|
|
|
],
|
|
|
|
},
|
|
|
|
}]
|
2007-10-22 01:26:32 +08:00
|
|
|
settings.LANGUAGE_CODE = 'en'
|
2007-12-02 05:58:51 +08:00
|
|
|
settings.SITE_ID = 1
|
2015-11-07 23:12:37 +08:00
|
|
|
settings.MIDDLEWARE = ALWAYS_MIDDLEWARE
|
2014-06-17 01:28:03 +08:00
|
|
|
settings.MIGRATION_MODULES = {
|
2016-01-26 03:29:24 +08:00
|
|
|
# This lets us skip creating migrations for the test models as many of
|
|
|
|
# them depend on one of the following contrib applications.
|
|
|
|
'auth': None,
|
|
|
|
'contenttypes': None,
|
|
|
|
'sessions': None,
|
2014-06-17 01:28:03 +08:00
|
|
|
}
|
2015-09-16 02:13:34 +08:00
|
|
|
log_config = copy.deepcopy(DEFAULT_LOGGING)
|
2015-03-24 06:17:43 +08:00
|
|
|
# Filter out non-error logging so we don't have to capture it in lots of
|
|
|
|
# tests.
|
|
|
|
log_config['loggers']['django']['level'] = 'ERROR'
|
|
|
|
settings.LOGGING = log_config
|
2016-03-18 22:24:29 +08:00
|
|
|
settings.SILENCED_SYSTEM_CHECKS = [
|
|
|
|
'fields.W342', # ForeignKey(unique=True) -> OneToOneField
|
|
|
|
]
|
2006-12-15 14:06:52 +08:00
|
|
|
|
|
|
|
# Load all the ALWAYS_INSTALLED_APPS.
|
2014-01-02 05:55:50 +08:00
|
|
|
django.setup()
|
2006-12-15 14:06:52 +08:00
|
|
|
|
2021-06-04 13:03:34 +08:00
|
|
|
# This flag must be evaluated after django.setup() because otherwise it can
|
|
|
|
# raise AppRegistryNotReady when running gis_tests in isolation on some
|
|
|
|
# backends (e.g. PostGIS).
|
2021-06-04 13:09:33 +08:00
|
|
|
gis_enabled = connection.features.gis_enabled
|
2017-05-31 03:14:32 +08:00
|
|
|
|
2021-06-04 13:03:34 +08:00
|
|
|
test_modules = list(get_filtered_test_modules(
|
|
|
|
start_at, start_after, gis_enabled, test_labels=test_labels,
|
|
|
|
))
|
|
|
|
return test_modules, state
|
2010-12-22 07:42:12 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2021-06-04 11:55:16 +08:00
|
|
|
def teardown_collect_tests(state):
|
|
|
|
# Restore the old settings.
|
|
|
|
for key, value in state.items():
|
|
|
|
setattr(settings, key, value)
|
|
|
|
|
|
|
|
|
2021-06-04 13:03:34 +08:00
|
|
|
def get_installed():
|
|
|
|
return [app_config.name for app_config in apps.get_app_configs()]
|
2015-02-10 08:20:39 +08:00
|
|
|
|
2021-06-04 13:03:34 +08:00
|
|
|
|
|
|
|
# This function should be called only after calling django.setup(),
|
|
|
|
# since it calls connection.features.gis_enabled.
|
|
|
|
def get_apps_to_install(test_modules):
|
|
|
|
for test_module in test_modules:
|
|
|
|
if test_module in CONTRIB_TESTS_TO_APPS:
|
|
|
|
yield from CONTRIB_TESTS_TO_APPS[test_module]
|
|
|
|
yield test_module
|
2014-05-24 16:53:22 +08:00
|
|
|
|
2015-02-10 23:07:44 +08:00
|
|
|
# Add contrib.gis to INSTALLED_APPS if needed (rather than requiring
|
|
|
|
# @override_settings(INSTALLED_APPS=...) on all test cases.
|
2021-06-04 13:03:34 +08:00
|
|
|
if connection.features.gis_enabled:
|
|
|
|
yield 'django.contrib.gis'
|
|
|
|
|
|
|
|
|
2021-06-04 11:56:22 +08:00
|
|
|
def setup_run_tests(verbosity, start_at, start_after, test_labels=None):
|
2021-06-04 13:03:34 +08:00
|
|
|
test_modules, state = setup_collect_tests(start_at, start_after, test_labels=test_labels)
|
|
|
|
|
|
|
|
installed_apps = set(get_installed())
|
|
|
|
for app in get_apps_to_install(test_modules):
|
|
|
|
if app in installed_apps:
|
|
|
|
continue
|
2015-02-10 23:07:44 +08:00
|
|
|
if verbosity >= 2:
|
2021-06-04 13:03:34 +08:00
|
|
|
print(f'Importing application {app}')
|
|
|
|
settings.INSTALLED_APPS.append(app)
|
|
|
|
installed_apps.add(app)
|
2015-02-10 23:07:44 +08:00
|
|
|
|
2014-05-24 16:53:22 +08:00
|
|
|
apps.set_installed_apps(settings.INSTALLED_APPS)
|
2006-08-27 21:59:47 +08:00
|
|
|
|
2021-06-04 13:03:34 +08:00
|
|
|
# Force declaring available_apps in TransactionTestCase for faster tests.
|
|
|
|
def no_available_apps(self):
|
|
|
|
raise Exception(
|
|
|
|
'Please define available_apps in TransactionTestCase and its '
|
|
|
|
'subclasses.'
|
|
|
|
)
|
|
|
|
TransactionTestCase.available_apps = property(no_available_apps)
|
|
|
|
TestCase.available_apps = None
|
|
|
|
|
2020-12-11 01:00:57 +08:00
|
|
|
# Set an environment variable that other code may consult to see if
|
|
|
|
# Django's own test suite is running.
|
|
|
|
os.environ['RUNNING_DJANGOS_TEST_SUITE'] = 'true'
|
|
|
|
|
2021-06-04 09:17:44 +08:00
|
|
|
test_labels = test_labels or test_modules
|
|
|
|
return test_labels, state
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2013-09-20 05:02:49 +08:00
|
|
|
|
2021-06-04 11:56:22 +08:00
|
|
|
def teardown_run_tests(state):
|
2021-06-04 11:55:16 +08:00
|
|
|
teardown_collect_tests(state)
|
2017-03-18 22:01:42 +08:00
|
|
|
# Discard the multiprocessing.util finalizer that tries to remove a
|
|
|
|
# temporary directory that's already removed by this script's
|
|
|
|
# atexit.register(shutil.rmtree, TMPDIR) handler. Prevents
|
2019-01-18 23:04:29 +08:00
|
|
|
# FileNotFoundError at the end of a test run (#27890).
|
2017-03-18 22:01:42 +08:00
|
|
|
from multiprocessing.util import _finalizer_registry
|
|
|
|
_finalizer_registry.pop((-100, 0), None)
|
2020-12-11 01:00:57 +08:00
|
|
|
del os.environ['RUNNING_DJANGOS_TEST_SUITE']
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2013-09-20 05:02:49 +08:00
|
|
|
|
2015-09-06 17:12:08 +08:00
|
|
|
def actual_test_processes(parallel):
|
|
|
|
if parallel == 0:
|
|
|
|
# This doesn't work before django.setup() on some databases.
|
2015-09-13 02:58:26 +08:00
|
|
|
if all(conn.features.can_clone_databases for conn in connections.all()):
|
2015-09-06 17:12:08 +08:00
|
|
|
return default_test_processes()
|
|
|
|
else:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return parallel
|
|
|
|
|
|
|
|
|
2016-02-07 10:24:36 +08:00
|
|
|
class ActionSelenium(argparse.Action):
|
|
|
|
"""
|
|
|
|
Validate the comma-separated list of requested browsers.
|
|
|
|
"""
|
|
|
|
def __call__(self, parser, namespace, values, option_string=None):
|
|
|
|
browsers = values.split(',')
|
|
|
|
for browser in browsers:
|
|
|
|
try:
|
|
|
|
SeleniumTestCaseBase.import_webdriver(browser)
|
|
|
|
except ImportError:
|
|
|
|
raise argparse.ArgumentError(self, "Selenium browser specification '%s' is not valid." % browser)
|
|
|
|
setattr(namespace, self.dest, browsers)
|
|
|
|
|
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
def django_tests(verbosity, interactive, failfast, keepdb, reverse,
|
2019-03-08 04:58:30 +08:00
|
|
|
test_labels, debug_sql, parallel, tags, exclude_tags,
|
2020-07-22 23:37:52 +08:00
|
|
|
test_name_patterns, start_at, start_after, pdb, buffer,
|
|
|
|
timing):
|
2021-04-07 13:16:35 +08:00
|
|
|
if verbosity >= 1:
|
|
|
|
msg = "Testing against Django installed in '%s'" % os.path.dirname(django.__file__)
|
|
|
|
max_parallel = default_test_processes() if parallel == 0 else parallel
|
|
|
|
if max_parallel > 1:
|
|
|
|
msg += " with up to %d processes" % max_parallel
|
|
|
|
print(msg)
|
|
|
|
|
2021-06-04 11:56:22 +08:00
|
|
|
test_labels, state = setup_run_tests(verbosity, start_at, start_after, test_labels)
|
2006-08-27 21:59:47 +08:00
|
|
|
# Run the test suite, including the extra validation tests.
|
2013-05-11 21:47:40 +08:00
|
|
|
if not hasattr(settings, 'TEST_RUNNER'):
|
|
|
|
settings.TEST_RUNNER = 'django.test.runner.DiscoverRunner'
|
|
|
|
TestRunner = get_runner(settings)
|
|
|
|
test_runner = TestRunner(
|
2013-05-11 11:08:45 +08:00
|
|
|
verbosity=verbosity,
|
|
|
|
interactive=interactive,
|
|
|
|
failfast=failfast,
|
2014-11-16 09:01:45 +08:00
|
|
|
keepdb=keepdb,
|
2014-11-23 00:59:05 +08:00
|
|
|
reverse=reverse,
|
2015-01-11 06:52:59 +08:00
|
|
|
debug_sql=debug_sql,
|
2015-09-06 17:12:08 +08:00
|
|
|
parallel=actual_test_processes(parallel),
|
2015-11-07 21:57:56 +08:00
|
|
|
tags=tags,
|
|
|
|
exclude_tags=exclude_tags,
|
2019-03-08 04:58:30 +08:00
|
|
|
test_name_patterns=test_name_patterns,
|
2019-08-05 11:41:14 +08:00
|
|
|
pdb=pdb,
|
2019-12-01 05:10:16 +08:00
|
|
|
buffer=buffer,
|
2020-07-22 23:37:52 +08:00
|
|
|
timing=timing,
|
2013-05-11 11:08:45 +08:00
|
|
|
)
|
2021-06-04 09:17:44 +08:00
|
|
|
failures = test_runner.run_tests(test_labels)
|
2021-06-04 11:56:22 +08:00
|
|
|
teardown_run_tests(state)
|
2010-10-09 22:44:54 +08:00
|
|
|
return failures
|
2006-12-15 14:06:52 +08:00
|
|
|
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2021-06-04 11:55:16 +08:00
|
|
|
def collect_test_modules(start_at, start_after):
|
|
|
|
test_modules, state = setup_collect_tests(start_at, start_after)
|
|
|
|
teardown_collect_tests(state)
|
|
|
|
return test_modules
|
2021-04-07 06:28:46 +08:00
|
|
|
|
|
|
|
|
2016-02-16 02:53:59 +08:00
|
|
|
def get_subprocess_args(options):
|
|
|
|
subprocess_args = [
|
2017-01-20 21:01:02 +08:00
|
|
|
sys.executable, __file__, '--settings=%s' % options.settings
|
2016-02-16 02:53:59 +08:00
|
|
|
]
|
|
|
|
if options.failfast:
|
|
|
|
subprocess_args.append('--failfast')
|
|
|
|
if options.verbosity:
|
|
|
|
subprocess_args.append('--verbosity=%s' % options.verbosity)
|
|
|
|
if not options.interactive:
|
|
|
|
subprocess_args.append('--noinput')
|
2015-11-07 21:57:56 +08:00
|
|
|
if options.tags:
|
|
|
|
subprocess_args.append('--tag=%s' % options.tags)
|
|
|
|
if options.exclude_tags:
|
|
|
|
subprocess_args.append('--exclude_tag=%s' % options.exclude_tags)
|
2016-02-16 02:53:59 +08:00
|
|
|
return subprocess_args
|
|
|
|
|
|
|
|
|
2021-04-07 13:16:35 +08:00
|
|
|
def bisect_tests(bisection_label, options, test_labels, start_at, start_after):
|
2021-04-07 06:28:46 +08:00
|
|
|
if not test_labels:
|
2021-06-04 11:55:16 +08:00
|
|
|
test_labels = collect_test_modules(start_at, start_after)
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2012-04-29 00:02:01 +08:00
|
|
|
print('***** Bisecting test suite: %s' % ' '.join(test_labels))
|
2010-10-09 22:44:54 +08:00
|
|
|
|
|
|
|
# Make sure the bisection point isn't in the test list
|
|
|
|
# Also remove tests that need to be run in specific combinations
|
|
|
|
for label in [bisection_label, 'model_inheritance_same_model_name']:
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2010-10-09 22:44:54 +08:00
|
|
|
test_labels.remove(label)
|
2017-09-07 20:16:21 +08:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2016-02-16 02:53:59 +08:00
|
|
|
subprocess_args = get_subprocess_args(options)
|
2010-10-09 22:44:54 +08:00
|
|
|
|
|
|
|
iteration = 1
|
|
|
|
while len(test_labels) > 1:
|
2013-09-24 05:05:36 +08:00
|
|
|
midpoint = len(test_labels) // 2
|
2010-10-09 22:44:54 +08:00
|
|
|
test_labels_a = test_labels[:midpoint] + [bisection_label]
|
|
|
|
test_labels_b = test_labels[midpoint:] + [bisection_label]
|
2012-04-29 00:02:01 +08:00
|
|
|
print('***** Pass %da: Running the first half of the test suite' % iteration)
|
|
|
|
print('***** Test labels: %s' % ' '.join(test_labels_a))
|
2019-08-23 16:53:36 +08:00
|
|
|
failures_a = subprocess.run(subprocess_args + test_labels_a)
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2012-04-29 00:02:01 +08:00
|
|
|
print('***** Pass %db: Running the second half of the test suite' % iteration)
|
|
|
|
print('***** Test labels: %s' % ' '.join(test_labels_b))
|
|
|
|
print('')
|
2019-08-23 16:53:36 +08:00
|
|
|
failures_b = subprocess.run(subprocess_args + test_labels_b)
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2019-08-23 16:53:36 +08:00
|
|
|
if failures_a.returncode and not failures_b.returncode:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("***** Problem found in first half. Bisecting again...")
|
2016-07-21 16:28:40 +08:00
|
|
|
iteration += 1
|
2010-10-09 22:44:54 +08:00
|
|
|
test_labels = test_labels_a[:-1]
|
2019-08-23 16:53:36 +08:00
|
|
|
elif failures_b.returncode and not failures_a.returncode:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("***** Problem found in second half. Bisecting again...")
|
2016-07-21 16:28:40 +08:00
|
|
|
iteration += 1
|
2010-10-09 22:44:54 +08:00
|
|
|
test_labels = test_labels_b[:-1]
|
2019-08-23 16:53:36 +08:00
|
|
|
elif failures_a.returncode and failures_b.returncode:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("***** Multiple sources of failure found")
|
2010-10-09 22:44:54 +08:00
|
|
|
break
|
|
|
|
else:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("***** No source of failure found... try pair execution (--pair)")
|
2010-10-09 22:44:54 +08:00
|
|
|
break
|
|
|
|
|
|
|
|
if len(test_labels) == 1:
|
2012-04-29 00:02:01 +08:00
|
|
|
print("***** Source of error: %s" % test_labels[0])
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2013-09-20 05:02:49 +08:00
|
|
|
|
2021-04-07 13:16:35 +08:00
|
|
|
def paired_tests(paired_test, options, test_labels, start_at, start_after):
|
2021-04-07 06:28:46 +08:00
|
|
|
if not test_labels:
|
2021-06-04 11:55:16 +08:00
|
|
|
test_labels = collect_test_modules(start_at, start_after)
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2012-04-29 00:02:01 +08:00
|
|
|
print('***** Trying paired execution')
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2010-10-11 20:55:17 +08:00
|
|
|
# Make sure the constant member of the pair isn't in the test list
|
2010-10-09 22:44:54 +08:00
|
|
|
# Also remove tests that need to be run in specific combinations
|
|
|
|
for label in [paired_test, 'model_inheritance_same_model_name']:
|
2017-09-07 20:16:21 +08:00
|
|
|
try:
|
2010-10-09 22:44:54 +08:00
|
|
|
test_labels.remove(label)
|
2017-09-07 20:16:21 +08:00
|
|
|
except ValueError:
|
|
|
|
pass
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2016-02-16 02:53:59 +08:00
|
|
|
subprocess_args = get_subprocess_args(options)
|
2010-10-09 22:44:54 +08:00
|
|
|
|
|
|
|
for i, label in enumerate(test_labels):
|
2012-04-29 00:02:01 +08:00
|
|
|
print('***** %d of %d: Check test pairing with %s' % (
|
|
|
|
i + 1, len(test_labels), label))
|
2010-10-09 22:44:54 +08:00
|
|
|
failures = subprocess.call(subprocess_args + [label, paired_test])
|
|
|
|
if failures:
|
2012-04-29 00:02:01 +08:00
|
|
|
print('***** Found problem pair with %s' % label)
|
2010-10-09 22:44:54 +08:00
|
|
|
return
|
|
|
|
|
2012-04-29 00:02:01 +08:00
|
|
|
print('***** No problem pair found')
|
2006-12-15 14:06:52 +08:00
|
|
|
|
2013-09-20 05:02:49 +08:00
|
|
|
|
2005-07-29 23:15:40 +08:00
|
|
|
if __name__ == "__main__":
|
2016-02-07 10:24:36 +08:00
|
|
|
parser = argparse.ArgumentParser(description="Run the Django test suite.")
|
2016-04-08 10:04:45 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'modules', nargs='*', metavar='module',
|
2014-06-07 03:12:18 +08:00
|
|
|
help='Optional path(s) to test modules; e.g. "i18n" or '
|
2016-04-08 10:04:45 +08:00
|
|
|
'"i18n.tests.TranslationTests.test_lazy_objects".',
|
|
|
|
)
|
2014-06-07 03:12:18 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'-v', '--verbosity', default=1, type=int, choices=[0, 1, 2, 3],
|
2016-04-08 10:04:45 +08:00
|
|
|
help='Verbosity level; 0=minimal output, 1=normal output, 2=all output',
|
|
|
|
)
|
2014-06-07 03:12:18 +08:00
|
|
|
parser.add_argument(
|
2017-04-02 08:03:56 +08:00
|
|
|
'--noinput', action='store_false', dest='interactive',
|
2016-04-08 10:04:45 +08:00
|
|
|
help='Tells Django to NOT prompt the user for input of any kind.',
|
|
|
|
)
|
2014-06-07 03:12:18 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--failfast', action='store_true',
|
2016-04-08 10:04:45 +08:00
|
|
|
help='Tells Django to stop running the test suite after first failed test.',
|
|
|
|
)
|
2014-11-16 09:01:45 +08:00
|
|
|
parser.add_argument(
|
2019-03-08 04:58:30 +08:00
|
|
|
'--keepdb', action='store_true',
|
2016-04-08 10:04:45 +08:00
|
|
|
help='Tells Django to preserve the test database between runs.',
|
|
|
|
)
|
2014-06-07 03:12:18 +08:00
|
|
|
parser.add_argument(
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
'--settings',
|
|
|
|
help='Python path to settings module, e.g. "myproject.settings". If '
|
2014-06-07 03:12:18 +08:00
|
|
|
'this isn\'t provided, either the DJANGO_SETTINGS_MODULE '
|
2016-04-08 10:04:45 +08:00
|
|
|
'environment variable or "test_sqlite" will be used.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--bisect',
|
Fixed #2879 -- Added support for the integration with Selenium and other in-browser testing frameworks. Also added the first Selenium tests for `contrib.admin`. Many thanks to everyone for their contributions and feedback: Mikeal Rogers, Dirk Datzert, mir, Simon G., Almad, Russell Keith-Magee, Denis Golomazov, devin, robertrv, andrewbadr, Idan Gazit, voidspace, Tom Christie, hjwp2, Adam Nelson, Jannis Leidel, Anssi Kääriäinen, Preston Holmes, Bruno Renié and Jacob Kaplan-Moss.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@17241 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2011-12-22 16:33:58 +08:00
|
|
|
help='Bisect the test suite to discover a test that causes a test '
|
2016-04-08 10:04:45 +08:00
|
|
|
'failure when combined with the named test.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--pair',
|
|
|
|
help='Run the test suite in pairs with the named test to find problem pairs.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2017-04-02 08:03:56 +08:00
|
|
|
'--reverse', action='store_true',
|
2014-11-23 00:59:05 +08:00
|
|
|
help='Sort test suites and test cases in opposite order to debug '
|
2016-04-08 10:04:45 +08:00
|
|
|
'test side effects not apparent with normal execution lineup.',
|
|
|
|
)
|
2014-06-07 03:12:18 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--selenium', action=ActionSelenium, metavar='BROWSERS',
|
2016-04-08 10:04:45 +08:00
|
|
|
help='A comma-separated list of browsers to run the Selenium tests against.',
|
|
|
|
)
|
2019-02-27 00:23:49 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--headless', action='store_true',
|
|
|
|
help='Run selenium tests in headless mode, if the browser supports the option.',
|
|
|
|
)
|
2018-10-26 01:52:29 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--selenium-hub',
|
|
|
|
help='A URL for a selenium hub instance to use in combination with --selenium.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--external-host', default=socket.gethostname(),
|
|
|
|
help='The external host that can be reached by the selenium hub instance when running Selenium '
|
|
|
|
'tests via Selenium Hub.',
|
|
|
|
)
|
2015-01-11 06:52:59 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--debug-sql', action='store_true',
|
2016-04-08 10:04:45 +08:00
|
|
|
help='Turn on the SQL query logger within tests.',
|
|
|
|
)
|
2015-02-06 03:31:02 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--parallel', nargs='?', default=0, type=int,
|
2015-11-28 01:44:11 +08:00
|
|
|
const=default_test_processes(), metavar='N',
|
2016-04-08 10:04:45 +08:00
|
|
|
help='Run tests using up to N parallel processes.',
|
|
|
|
)
|
2015-11-07 21:57:56 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--tag', dest='tags', action='append',
|
2016-04-08 10:04:45 +08:00
|
|
|
help='Run only tests with the specified tags. Can be used multiple times.',
|
|
|
|
)
|
2015-11-07 21:57:56 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--exclude-tag', dest='exclude_tags', action='append',
|
2016-04-08 10:04:45 +08:00
|
|
|
help='Do not run tests with the specified tag. Can be used multiple times.',
|
|
|
|
)
|
2019-05-26 02:55:30 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--start-after', dest='start_after',
|
|
|
|
help='Run tests starting after the specified top-level module.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--start-at', dest='start_at',
|
|
|
|
help='Run tests starting at the specified top-level module.',
|
|
|
|
)
|
2019-08-05 11:41:14 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--pdb', action='store_true',
|
|
|
|
help='Runs the PDB debugger on error or failure.'
|
|
|
|
)
|
2019-12-01 05:10:16 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'-b', '--buffer', action='store_true',
|
|
|
|
help='Discard output of passing tests.',
|
|
|
|
)
|
2020-07-22 23:37:52 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'--timing', action='store_true',
|
|
|
|
help='Output timings, including database set up and total run time.',
|
|
|
|
)
|
2021-01-19 15:35:16 +08:00
|
|
|
parser.add_argument(
|
|
|
|
'-k', dest='test_name_patterns', action='append',
|
|
|
|
help=(
|
|
|
|
'Only run test methods and classes matching test name pattern. '
|
|
|
|
'Same as unittest -k option. Can be used multiple times.'
|
|
|
|
),
|
|
|
|
)
|
2015-02-06 03:31:02 +08:00
|
|
|
|
2014-06-07 03:12:18 +08:00
|
|
|
options = parser.parse_args()
|
2014-06-11 23:07:33 +08:00
|
|
|
|
2018-10-26 01:52:29 +08:00
|
|
|
using_selenium_hub = options.selenium and options.selenium_hub
|
|
|
|
if options.selenium_hub and not options.selenium:
|
|
|
|
parser.error('--selenium-hub and --external-host require --selenium to be used.')
|
|
|
|
if using_selenium_hub and not options.external_host:
|
|
|
|
parser.error('--selenium-hub and --external-host must be used together.')
|
|
|
|
|
2014-06-11 23:07:33 +08:00
|
|
|
# Allow including a trailing slash on app_labels for tab completion convenience
|
|
|
|
options.modules = [os.path.normpath(labels) for labels in options.modules]
|
|
|
|
|
2019-05-26 02:55:30 +08:00
|
|
|
mutually_exclusive_options = [options.start_at, options.start_after, options.modules]
|
|
|
|
enabled_module_options = [bool(option) for option in mutually_exclusive_options].count(True)
|
|
|
|
if enabled_module_options > 1:
|
|
|
|
print('Aborting: --start-at, --start-after, and test labels are mutually exclusive.')
|
|
|
|
sys.exit(1)
|
|
|
|
for opt_name in ['start_at', 'start_after']:
|
|
|
|
opt_val = getattr(options, opt_name)
|
|
|
|
if opt_val:
|
|
|
|
if '.' in opt_val:
|
|
|
|
print('Aborting: --%s must be a top-level module.' % opt_name.replace('_', '-'))
|
|
|
|
sys.exit(1)
|
|
|
|
setattr(options, opt_name, os.path.normpath(opt_val))
|
2005-08-10 23:36:16 +08:00
|
|
|
if options.settings:
|
|
|
|
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
|
2011-01-27 08:00:20 +08:00
|
|
|
else:
|
2017-11-14 05:15:49 +08:00
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'test_sqlite')
|
2011-01-27 08:00:20 +08:00
|
|
|
options.settings = os.environ['DJANGO_SETTINGS_MODULE']
|
2010-10-09 22:44:54 +08:00
|
|
|
|
2013-02-26 01:14:42 +08:00
|
|
|
if options.selenium:
|
2016-02-20 02:56:13 +08:00
|
|
|
if not options.tags:
|
|
|
|
options.tags = ['selenium']
|
|
|
|
elif 'selenium' not in options.tags:
|
|
|
|
options.tags.append('selenium')
|
2018-10-26 01:52:29 +08:00
|
|
|
if options.selenium_hub:
|
|
|
|
SeleniumTestCaseBase.selenium_hub = options.selenium_hub
|
|
|
|
SeleniumTestCaseBase.external_host = options.external_host
|
2019-02-27 00:23:49 +08:00
|
|
|
SeleniumTestCaseBase.headless = options.headless
|
2016-02-07 10:24:36 +08:00
|
|
|
SeleniumTestCaseBase.browsers = options.selenium
|
2013-02-24 00:10:48 +08:00
|
|
|
|
2010-10-09 22:44:54 +08:00
|
|
|
if options.bisect:
|
2019-05-26 02:55:30 +08:00
|
|
|
bisect_tests(
|
2021-04-07 13:16:35 +08:00
|
|
|
options.bisect, options, options.modules, options.start_at,
|
|
|
|
options.start_after,
|
2019-05-26 02:55:30 +08:00
|
|
|
)
|
2010-10-09 22:44:54 +08:00
|
|
|
elif options.pair:
|
2019-05-26 02:55:30 +08:00
|
|
|
paired_tests(
|
2021-04-07 13:16:35 +08:00
|
|
|
options.pair, options, options.modules, options.start_at,
|
|
|
|
options.start_after,
|
2019-05-26 02:55:30 +08:00
|
|
|
)
|
2010-10-09 22:44:54 +08:00
|
|
|
else:
|
2020-07-22 23:37:52 +08:00
|
|
|
time_keeper = TimeKeeper() if options.timing else NullTimeKeeper()
|
|
|
|
with time_keeper.timed('Total run'):
|
|
|
|
failures = django_tests(
|
|
|
|
options.verbosity, options.interactive, options.failfast,
|
|
|
|
options.keepdb, options.reverse, options.modules,
|
|
|
|
options.debug_sql, options.parallel, options.tags,
|
|
|
|
options.exclude_tags,
|
|
|
|
getattr(options, 'test_name_patterns', None),
|
|
|
|
options.start_at, options.start_after, options.pdb, options.buffer,
|
|
|
|
options.timing,
|
|
|
|
)
|
|
|
|
time_keeper.print_results()
|
2010-10-09 22:44:54 +08:00
|
|
|
if failures:
|
2016-11-03 19:40:59 +08:00
|
|
|
sys.exit(1)
|