2015-02-10 05:00:09 +08:00
|
|
|
import ctypes
|
2015-02-06 03:31:02 +08:00
|
|
|
import itertools
|
2015-01-11 06:52:59 +08:00
|
|
|
import logging
|
2015-02-06 03:31:02 +08:00
|
|
|
import multiprocessing
|
2013-05-11 11:08:45 +08:00
|
|
|
import os
|
2015-06-05 22:44:12 +08:00
|
|
|
import pickle
|
|
|
|
import textwrap
|
2013-07-01 20:22:27 +08:00
|
|
|
import unittest
|
2015-01-28 20:35:27 +08:00
|
|
|
from importlib import import_module
|
2017-01-07 19:11:46 +08:00
|
|
|
from io import StringIO
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2016-03-18 22:24:13 +08:00
|
|
|
from django.core.management import call_command
|
2016-07-03 06:20:14 +08:00
|
|
|
from django.db import connections
|
2018-12-06 04:21:09 +08:00
|
|
|
from django.test import SimpleTestCase, TestCase
|
2016-07-03 06:20:14 +08:00
|
|
|
from django.test.utils import (
|
|
|
|
setup_databases as _setup_databases, setup_test_environment,
|
|
|
|
teardown_databases as _teardown_databases, teardown_test_environment,
|
|
|
|
)
|
2014-12-14 20:37:26 +08:00
|
|
|
from django.utils.datastructures import OrderedSet
|
2019-03-08 04:58:30 +08:00
|
|
|
from django.utils.version import PY37
|
2015-01-11 06:52:59 +08:00
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
try:
|
|
|
|
import tblib.pickling_support
|
|
|
|
except ImportError:
|
|
|
|
tblib = None
|
|
|
|
|
2015-01-11 06:52:59 +08:00
|
|
|
|
|
|
|
class DebugSQLTextTestResult(unittest.TextTestResult):
|
|
|
|
def __init__(self, stream, descriptions, verbosity):
|
|
|
|
self.logger = logging.getLogger('django.db.backends')
|
|
|
|
self.logger.setLevel(logging.DEBUG)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__(stream, descriptions, verbosity)
|
2015-01-11 06:52:59 +08:00
|
|
|
|
|
|
|
def startTest(self, test):
|
|
|
|
self.debug_sql_stream = StringIO()
|
|
|
|
self.handler = logging.StreamHandler(self.debug_sql_stream)
|
|
|
|
self.logger.addHandler(self.handler)
|
2017-01-21 21:13:44 +08:00
|
|
|
super().startTest(test)
|
2015-01-11 06:52:59 +08:00
|
|
|
|
|
|
|
def stopTest(self, test):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().stopTest(test)
|
2015-01-11 06:52:59 +08:00
|
|
|
self.logger.removeHandler(self.handler)
|
|
|
|
if self.showAll:
|
|
|
|
self.debug_sql_stream.seek(0)
|
|
|
|
self.stream.write(self.debug_sql_stream.read())
|
|
|
|
self.stream.writeln(self.separator2)
|
|
|
|
|
|
|
|
def addError(self, test, err):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().addError(test, err)
|
2015-01-11 06:52:59 +08:00
|
|
|
self.debug_sql_stream.seek(0)
|
|
|
|
self.errors[-1] = self.errors[-1] + (self.debug_sql_stream.read(),)
|
|
|
|
|
|
|
|
def addFailure(self, test, err):
|
2017-01-21 21:13:44 +08:00
|
|
|
super().addFailure(test, err)
|
2015-01-11 06:52:59 +08:00
|
|
|
self.debug_sql_stream.seek(0)
|
|
|
|
self.failures[-1] = self.failures[-1] + (self.debug_sql_stream.read(),)
|
|
|
|
|
2017-07-05 13:17:16 +08:00
|
|
|
def addSubTest(self, test, subtest, err):
|
|
|
|
super().addSubTest(test, subtest, err)
|
|
|
|
if err is not None:
|
|
|
|
self.debug_sql_stream.seek(0)
|
|
|
|
errors = self.failures if issubclass(err[0], test.failureException) else self.errors
|
|
|
|
errors[-1] = errors[-1] + (self.debug_sql_stream.read(),)
|
|
|
|
|
2015-01-11 06:52:59 +08:00
|
|
|
def printErrorList(self, flavour, errors):
|
|
|
|
for test, err, sql_debug in errors:
|
|
|
|
self.stream.writeln(self.separator1)
|
|
|
|
self.stream.writeln("%s: %s" % (flavour, self.getDescription(test)))
|
|
|
|
self.stream.writeln(self.separator2)
|
2019-01-15 10:04:00 +08:00
|
|
|
self.stream.writeln(err)
|
2015-01-11 06:52:59 +08:00
|
|
|
self.stream.writeln(self.separator2)
|
2019-01-15 10:04:00 +08:00
|
|
|
self.stream.writeln(sql_debug)
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class RemoteTestResult:
|
2015-02-06 03:31:02 +08:00
|
|
|
"""
|
|
|
|
Record information about which tests have succeeded and which have failed.
|
|
|
|
|
|
|
|
The sole purpose of this class is to record events in the child processes
|
|
|
|
so they can be replayed in the master process. As a consequence it doesn't
|
|
|
|
inherit unittest.TestResult and doesn't attempt to implement all its API.
|
|
|
|
|
|
|
|
The implementation matches the unpythonic coding style of unittest2.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2016-08-11 18:47:12 +08:00
|
|
|
if tblib is not None:
|
|
|
|
tblib.pickling_support.install()
|
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
self.events = []
|
|
|
|
self.failfast = False
|
|
|
|
self.shouldStop = False
|
|
|
|
self.testsRun = 0
|
|
|
|
|
|
|
|
@property
|
|
|
|
def test_index(self):
|
|
|
|
return self.testsRun - 1
|
|
|
|
|
2016-10-07 03:54:38 +08:00
|
|
|
def _confirm_picklable(self, obj):
|
|
|
|
"""
|
|
|
|
Confirm that obj can be pickled and unpickled as multiprocessing will
|
|
|
|
need to pickle the exception in the child process and unpickle it in
|
|
|
|
the parent process. Let the exception rise, if not.
|
|
|
|
"""
|
|
|
|
pickle.loads(pickle.dumps(obj))
|
|
|
|
|
2016-08-11 18:47:12 +08:00
|
|
|
def _print_unpicklable_subtest(self, test, subtest, pickle_exc):
|
|
|
|
print("""
|
|
|
|
Subtest failed:
|
|
|
|
|
|
|
|
test: {}
|
|
|
|
subtest: {}
|
|
|
|
|
|
|
|
Unfortunately, the subtest that failed cannot be pickled, so the parallel
|
|
|
|
test runner cannot handle it cleanly. Here is the pickling error:
|
|
|
|
|
|
|
|
> {}
|
|
|
|
|
|
|
|
You should re-run this test with --parallel=1 to reproduce the failure
|
|
|
|
with a cleaner failure message.
|
|
|
|
""".format(test, subtest, pickle_exc))
|
|
|
|
|
2015-12-03 07:55:50 +08:00
|
|
|
def check_picklable(self, test, err):
|
2015-06-05 22:44:12 +08:00
|
|
|
# Ensure that sys.exc_info() tuples are picklable. This displays a
|
|
|
|
# clear multiprocessing.pool.RemoteTraceback generated in the child
|
|
|
|
# process instead of a multiprocessing.pool.MaybeEncodingError, making
|
|
|
|
# the root cause easier to figure out for users who aren't familiar
|
|
|
|
# with the multiprocessing module. Since we're in a forked process,
|
|
|
|
# our best chance to communicate with them is to print to stdout.
|
|
|
|
try:
|
2016-10-07 03:54:38 +08:00
|
|
|
self._confirm_picklable(err)
|
2015-06-05 22:44:12 +08:00
|
|
|
except Exception as exc:
|
|
|
|
original_exc_txt = repr(err[1])
|
2015-09-11 00:23:41 +08:00
|
|
|
original_exc_txt = textwrap.fill(original_exc_txt, 75, initial_indent=' ', subsequent_indent=' ')
|
2015-06-05 22:44:12 +08:00
|
|
|
pickle_exc_txt = repr(exc)
|
2015-09-11 00:23:41 +08:00
|
|
|
pickle_exc_txt = textwrap.fill(pickle_exc_txt, 75, initial_indent=' ', subsequent_indent=' ')
|
2015-06-05 22:44:12 +08:00
|
|
|
if tblib is None:
|
|
|
|
print("""
|
|
|
|
|
|
|
|
{} failed:
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
Unfortunately, tracebacks cannot be pickled, making it impossible for the
|
|
|
|
parallel test runner to handle this exception cleanly.
|
|
|
|
|
|
|
|
In order to see the traceback, you should install tblib:
|
|
|
|
|
2019-04-14 23:02:36 +08:00
|
|
|
python -m pip install tblib
|
2015-06-05 22:44:12 +08:00
|
|
|
""".format(test, original_exc_txt))
|
|
|
|
else:
|
|
|
|
print("""
|
|
|
|
|
|
|
|
{} failed:
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
Unfortunately, the exception it raised cannot be pickled, making it impossible
|
|
|
|
for the parallel test runner to handle it cleanly.
|
|
|
|
|
|
|
|
Here's the error encountered while trying to pickle the exception:
|
|
|
|
|
|
|
|
{}
|
|
|
|
|
2016-08-08 21:24:14 +08:00
|
|
|
You should re-run this test with the --parallel=1 option to reproduce the
|
2015-06-05 22:44:12 +08:00
|
|
|
failure and get a correct traceback.
|
|
|
|
""".format(test, original_exc_txt, pickle_exc_txt))
|
|
|
|
raise
|
|
|
|
|
2016-08-11 18:47:12 +08:00
|
|
|
def check_subtest_picklable(self, test, subtest):
|
|
|
|
try:
|
2016-10-07 03:54:38 +08:00
|
|
|
self._confirm_picklable(subtest)
|
2016-08-11 18:47:12 +08:00
|
|
|
except Exception as exc:
|
|
|
|
self._print_unpicklable_subtest(test, subtest, exc)
|
|
|
|
raise
|
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
def stop_if_failfast(self):
|
|
|
|
if self.failfast:
|
|
|
|
self.stop()
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
self.shouldStop = True
|
|
|
|
|
|
|
|
def startTestRun(self):
|
|
|
|
self.events.append(('startTestRun',))
|
|
|
|
|
|
|
|
def stopTestRun(self):
|
|
|
|
self.events.append(('stopTestRun',))
|
|
|
|
|
|
|
|
def startTest(self, test):
|
|
|
|
self.testsRun += 1
|
|
|
|
self.events.append(('startTest', self.test_index))
|
|
|
|
|
|
|
|
def stopTest(self, test):
|
|
|
|
self.events.append(('stopTest', self.test_index))
|
|
|
|
|
|
|
|
def addError(self, test, err):
|
2015-12-03 07:55:50 +08:00
|
|
|
self.check_picklable(test, err)
|
2015-02-06 03:31:02 +08:00
|
|
|
self.events.append(('addError', self.test_index, err))
|
|
|
|
self.stop_if_failfast()
|
|
|
|
|
|
|
|
def addFailure(self, test, err):
|
2015-12-03 07:55:50 +08:00
|
|
|
self.check_picklable(test, err)
|
2015-02-06 03:31:02 +08:00
|
|
|
self.events.append(('addFailure', self.test_index, err))
|
|
|
|
self.stop_if_failfast()
|
|
|
|
|
|
|
|
def addSubTest(self, test, subtest, err):
|
2016-08-11 18:47:12 +08:00
|
|
|
# Follow Python 3.5's implementation of unittest.TestResult.addSubTest()
|
|
|
|
# by not doing anything when a subtest is successful.
|
|
|
|
if err is not None:
|
|
|
|
# Call check_picklable() before check_subtest_picklable() since
|
|
|
|
# check_picklable() performs the tblib check.
|
|
|
|
self.check_picklable(test, err)
|
|
|
|
self.check_subtest_picklable(test, subtest)
|
|
|
|
self.events.append(('addSubTest', self.test_index, subtest, err))
|
|
|
|
self.stop_if_failfast()
|
2015-02-06 03:31:02 +08:00
|
|
|
|
|
|
|
def addSuccess(self, test):
|
|
|
|
self.events.append(('addSuccess', self.test_index))
|
|
|
|
|
|
|
|
def addSkip(self, test, reason):
|
|
|
|
self.events.append(('addSkip', self.test_index, reason))
|
|
|
|
|
|
|
|
def addExpectedFailure(self, test, err):
|
2015-09-11 00:05:15 +08:00
|
|
|
# If tblib isn't installed, pickling the traceback will always fail.
|
|
|
|
# However we don't want tblib to be required for running the tests
|
|
|
|
# when they pass or fail as expected. Drop the traceback when an
|
|
|
|
# expected failure occurs.
|
|
|
|
if tblib is None:
|
|
|
|
err = err[0], err[1], None
|
2015-12-03 07:55:50 +08:00
|
|
|
self.check_picklable(test, err)
|
2015-02-06 03:31:02 +08:00
|
|
|
self.events.append(('addExpectedFailure', self.test_index, err))
|
|
|
|
|
|
|
|
def addUnexpectedSuccess(self, test):
|
|
|
|
self.events.append(('addUnexpectedSuccess', self.test_index))
|
|
|
|
self.stop_if_failfast()
|
|
|
|
|
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class RemoteTestRunner:
|
2015-02-06 03:31:02 +08:00
|
|
|
"""
|
|
|
|
Run tests and record everything but don't display anything.
|
|
|
|
|
|
|
|
The implementation matches the unpythonic coding style of unittest2.
|
|
|
|
"""
|
|
|
|
|
|
|
|
resultclass = RemoteTestResult
|
|
|
|
|
|
|
|
def __init__(self, failfast=False, resultclass=None):
|
|
|
|
self.failfast = failfast
|
|
|
|
if resultclass is not None:
|
|
|
|
self.resultclass = resultclass
|
|
|
|
|
|
|
|
def run(self, test):
|
|
|
|
result = self.resultclass()
|
|
|
|
unittest.registerResult(result)
|
|
|
|
result.failfast = self.failfast
|
|
|
|
test(result)
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
def default_test_processes():
|
2017-01-25 04:37:33 +08:00
|
|
|
"""Default number of test processes when using the --parallel option."""
|
2015-09-13 02:58:26 +08:00
|
|
|
# The current implementation of the parallel test runner requires
|
|
|
|
# multiprocessing to start subprocesses with fork().
|
2017-01-26 02:59:25 +08:00
|
|
|
if multiprocessing.get_start_method() != 'fork':
|
2015-09-13 02:58:26 +08:00
|
|
|
return 1
|
2015-02-06 03:31:02 +08:00
|
|
|
try:
|
|
|
|
return int(os.environ['DJANGO_TEST_PROCESSES'])
|
|
|
|
except KeyError:
|
|
|
|
return multiprocessing.cpu_count()
|
|
|
|
|
|
|
|
|
2015-02-10 05:00:09 +08:00
|
|
|
_worker_id = 0
|
|
|
|
|
|
|
|
|
|
|
|
def _init_worker(counter):
|
|
|
|
"""
|
|
|
|
Switch to databases dedicated to this worker.
|
|
|
|
|
|
|
|
This helper lives at module-level because of the multiprocessing module's
|
|
|
|
requirements.
|
|
|
|
"""
|
|
|
|
|
|
|
|
global _worker_id
|
|
|
|
|
|
|
|
with counter.get_lock():
|
|
|
|
counter.value += 1
|
|
|
|
_worker_id = counter.value
|
|
|
|
|
|
|
|
for alias in connections:
|
|
|
|
connection = connections[alias]
|
2017-09-11 23:32:40 +08:00
|
|
|
settings_dict = connection.creation.get_test_db_clone_settings(str(_worker_id))
|
2015-09-08 04:59:13 +08:00
|
|
|
# connection.settings_dict must be updated in place for changes to be
|
|
|
|
# reflected in django.db.connections. If the following line assigned
|
|
|
|
# connection.settings_dict = settings_dict, new threads would connect
|
|
|
|
# to the default database instead of the appropriate clone.
|
|
|
|
connection.settings_dict.update(settings_dict)
|
2015-02-10 05:00:09 +08:00
|
|
|
connection.close()
|
|
|
|
|
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
def _run_subsuite(args):
|
|
|
|
"""
|
|
|
|
Run a suite of tests with a RemoteTestRunner and return a RemoteTestResult.
|
|
|
|
|
|
|
|
This helper lives at module-level and its arguments are wrapped in a tuple
|
2015-02-10 05:00:09 +08:00
|
|
|
because of the multiprocessing module's requirements.
|
2015-02-06 03:31:02 +08:00
|
|
|
"""
|
2016-08-03 02:15:06 +08:00
|
|
|
runner_class, subsuite_index, subsuite, failfast = args
|
|
|
|
runner = runner_class(failfast=failfast)
|
2015-02-06 03:31:02 +08:00
|
|
|
result = runner.run(subsuite)
|
|
|
|
return subsuite_index, result.events
|
|
|
|
|
|
|
|
|
|
|
|
class ParallelTestSuite(unittest.TestSuite):
|
|
|
|
"""
|
|
|
|
Run a series of tests in parallel in several processes.
|
|
|
|
|
|
|
|
While the unittest module's documentation implies that orchestrating the
|
|
|
|
execution of tests is the responsibility of the test runner, in practice,
|
|
|
|
it appears that TestRunner classes are more concerned with formatting and
|
|
|
|
displaying test results.
|
|
|
|
|
|
|
|
Since there are fewer use cases for customizing TestSuite than TestRunner,
|
|
|
|
implementing parallelization at the level of the TestSuite improves
|
|
|
|
interoperability with existing custom test runners. A single instance of a
|
|
|
|
test runner can still collect results from all tests without being aware
|
|
|
|
that they have been run in parallel.
|
|
|
|
"""
|
|
|
|
|
2015-09-05 05:22:56 +08:00
|
|
|
# In case someone wants to modify these in a subclass.
|
|
|
|
init_worker = _init_worker
|
|
|
|
run_subsuite = _run_subsuite
|
2016-08-03 02:15:06 +08:00
|
|
|
runner_class = RemoteTestRunner
|
2015-09-05 05:22:56 +08:00
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
def __init__(self, suite, processes, failfast=False):
|
|
|
|
self.subsuites = partition_suite_by_case(suite)
|
|
|
|
self.processes = processes
|
|
|
|
self.failfast = failfast
|
2017-01-21 21:13:44 +08:00
|
|
|
super().__init__()
|
2015-02-06 03:31:02 +08:00
|
|
|
|
|
|
|
def run(self, result):
|
|
|
|
"""
|
|
|
|
Distribute test cases across workers.
|
|
|
|
|
|
|
|
Return an identifier of each test case with its result in order to use
|
|
|
|
imap_unordered to show results as soon as they're available.
|
|
|
|
|
|
|
|
To minimize pickling errors when getting results from workers:
|
|
|
|
|
|
|
|
- pass back numeric indexes in self.subsuites instead of tests
|
2015-12-03 07:55:50 +08:00
|
|
|
- make tracebacks picklable with tblib, if available
|
2015-02-06 03:31:02 +08:00
|
|
|
|
|
|
|
Even with tblib, errors may still occur for dynamically created
|
2017-10-13 21:16:09 +08:00
|
|
|
exception classes which cannot be unpickled.
|
2015-02-06 03:31:02 +08:00
|
|
|
"""
|
2015-02-10 05:00:09 +08:00
|
|
|
counter = multiprocessing.Value(ctypes.c_int, 0)
|
|
|
|
pool = multiprocessing.Pool(
|
|
|
|
processes=self.processes,
|
2015-09-05 05:22:56 +08:00
|
|
|
initializer=self.init_worker.__func__,
|
2018-03-16 17:54:34 +08:00
|
|
|
initargs=[counter],
|
|
|
|
)
|
2015-02-06 03:31:02 +08:00
|
|
|
args = [
|
2016-08-03 02:15:06 +08:00
|
|
|
(self.runner_class, index, subsuite, self.failfast)
|
2015-02-06 03:31:02 +08:00
|
|
|
for index, subsuite in enumerate(self.subsuites)
|
|
|
|
]
|
2015-09-05 05:22:56 +08:00
|
|
|
test_results = pool.imap_unordered(self.run_subsuite.__func__, args)
|
2015-02-06 03:31:02 +08:00
|
|
|
|
|
|
|
while True:
|
|
|
|
if result.shouldStop:
|
|
|
|
pool.terminate()
|
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
|
|
|
subsuite_index, events = test_results.next(timeout=0.1)
|
|
|
|
except multiprocessing.TimeoutError:
|
|
|
|
continue
|
|
|
|
except StopIteration:
|
|
|
|
pool.close()
|
|
|
|
break
|
|
|
|
|
|
|
|
tests = list(self.subsuites[subsuite_index])
|
|
|
|
for event in events:
|
|
|
|
event_name = event[0]
|
|
|
|
handler = getattr(result, event_name, None)
|
|
|
|
if handler is None:
|
|
|
|
continue
|
|
|
|
test = tests[event[1]]
|
|
|
|
args = event[2:]
|
|
|
|
handler(test, *args)
|
|
|
|
|
|
|
|
pool.join()
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
2018-07-12 12:14:24 +08:00
|
|
|
def __iter__(self):
|
|
|
|
return iter(self.subsuites)
|
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
|
2017-01-19 15:39:46 +08:00
|
|
|
class DiscoverRunner:
|
2017-01-25 04:37:33 +08:00
|
|
|
"""A Django test runner that uses unittest2 test discovery."""
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
test_suite = unittest.TestSuite
|
2015-09-05 05:22:56 +08:00
|
|
|
parallel_test_suite = ParallelTestSuite
|
2013-09-10 21:49:39 +08:00
|
|
|
test_runner = unittest.TextTestRunner
|
2015-02-06 03:31:02 +08:00
|
|
|
test_loader = unittest.defaultTestLoader
|
2018-12-06 04:21:09 +08:00
|
|
|
reorder_by = (TestCase, SimpleTestCase)
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2015-01-11 06:52:59 +08:00
|
|
|
def __init__(self, pattern=None, top_level=None, verbosity=1,
|
|
|
|
interactive=True, failfast=False, keepdb=False,
|
2016-08-09 16:40:40 +08:00
|
|
|
reverse=False, debug_mode=False, debug_sql=False, parallel=0,
|
2019-03-08 04:58:30 +08:00
|
|
|
tags=None, exclude_tags=None, test_name_patterns=None, **kwargs):
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
self.pattern = pattern
|
|
|
|
self.top_level = top_level
|
|
|
|
self.verbosity = verbosity
|
|
|
|
self.interactive = interactive
|
|
|
|
self.failfast = failfast
|
2014-05-28 05:13:08 +08:00
|
|
|
self.keepdb = keepdb
|
2014-11-23 00:59:05 +08:00
|
|
|
self.reverse = reverse
|
2016-08-09 16:40:40 +08:00
|
|
|
self.debug_mode = debug_mode
|
2015-01-11 06:52:59 +08:00
|
|
|
self.debug_sql = debug_sql
|
2015-02-06 03:31:02 +08:00
|
|
|
self.parallel = parallel
|
2015-11-07 21:57:56 +08:00
|
|
|
self.tags = set(tags or [])
|
|
|
|
self.exclude_tags = set(exclude_tags or [])
|
2019-03-08 04:58:30 +08:00
|
|
|
self.test_name_patterns = None
|
|
|
|
if test_name_patterns:
|
|
|
|
# unittest does not export the _convert_select_pattern function
|
|
|
|
# that converts command-line arguments to patterns.
|
|
|
|
self.test_name_patterns = {
|
|
|
|
pattern if '*' in pattern else '*%s*' % pattern
|
|
|
|
for pattern in test_name_patterns
|
|
|
|
}
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2014-06-07 02:12:23 +08:00
|
|
|
@classmethod
|
|
|
|
def add_arguments(cls, parser):
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'-t', '--top-level-directory', dest='top_level',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Top level of project for unittest discovery.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'-p', '--pattern', default="test*.py",
|
2016-03-29 06:33:29 +08:00
|
|
|
help='The test matching pattern. Defaults to test*.py.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2019-03-08 04:58:30 +08:00
|
|
|
'--keepdb', action='store_true',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Preserves the test DB between runs.'
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'-r', '--reverse', action='store_true',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Reverses test cases order.',
|
|
|
|
)
|
2016-08-11 01:46:54 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--debug-mode', action='store_true',
|
2016-08-11 01:46:54 +08:00
|
|
|
help='Sets settings.DEBUG to True.',
|
|
|
|
)
|
2016-03-29 06:33:29 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'-d', '--debug-sql', action='store_true',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Prints logged SQL queries on failure.',
|
|
|
|
)
|
2015-02-06 03:31:02 +08:00
|
|
|
parser.add_argument(
|
2018-07-03 05:54:57 +08:00
|
|
|
'--parallel', nargs='?', default=1, type=int,
|
2015-11-28 06:08:34 +08:00
|
|
|
const=default_test_processes(), metavar='N',
|
2016-03-29 06:33:29 +08:00
|
|
|
help='Run tests using up to N parallel processes.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--tag', action='append', dest='tags',
|
|
|
|
help='Run only tests with the specified tag. Can be used multiple times.',
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
'--exclude-tag', action='append', dest='exclude_tags',
|
|
|
|
help='Do not run tests with the specified tag. Can be used multiple times.',
|
|
|
|
)
|
2019-03-08 04:58:30 +08:00
|
|
|
if PY37:
|
|
|
|
parser.add_argument(
|
|
|
|
'-k', action='append', dest='test_name_patterns',
|
|
|
|
help=(
|
|
|
|
'Only run test methods and classes that match the pattern '
|
|
|
|
'or substring. Can be used multiple times. Same as '
|
|
|
|
'unittest -k option.'
|
|
|
|
),
|
|
|
|
)
|
2014-06-07 02:12:23 +08:00
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
def setup_test_environment(self, **kwargs):
|
2016-08-08 18:04:27 +08:00
|
|
|
setup_test_environment(debug=self.debug_mode)
|
2013-05-11 11:08:45 +08:00
|
|
|
unittest.installHandler()
|
|
|
|
|
|
|
|
def build_suite(self, test_labels=None, extra_tests=None, **kwargs):
|
2013-09-10 21:49:39 +08:00
|
|
|
suite = self.test_suite()
|
2013-05-11 11:08:45 +08:00
|
|
|
test_labels = test_labels or ['.']
|
|
|
|
extra_tests = extra_tests or []
|
2019-03-08 04:58:30 +08:00
|
|
|
self.test_loader.testNamePatterns = self.test_name_patterns
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
discover_kwargs = {}
|
|
|
|
if self.pattern is not None:
|
|
|
|
discover_kwargs['pattern'] = self.pattern
|
|
|
|
if self.top_level is not None:
|
|
|
|
discover_kwargs['top_level_dir'] = self.top_level
|
|
|
|
|
|
|
|
for label in test_labels:
|
|
|
|
kwargs = discover_kwargs.copy()
|
|
|
|
tests = None
|
|
|
|
|
|
|
|
label_as_path = os.path.abspath(label)
|
|
|
|
|
|
|
|
# if a module, or "module.ClassName[.method_name]", just run those
|
|
|
|
if not os.path.exists(label_as_path):
|
|
|
|
tests = self.test_loader.loadTestsFromName(label)
|
|
|
|
elif os.path.isdir(label_as_path) and not self.top_level:
|
|
|
|
# Try to be a bit smarter than unittest about finding the
|
|
|
|
# default top-level for a given directory path, to avoid
|
|
|
|
# breaking relative imports. (Unittest's default is to set
|
|
|
|
# top-level equal to the path, which means relative imports
|
|
|
|
# will result in "Attempted relative import in non-package.").
|
|
|
|
|
|
|
|
# We'd be happy to skip this and require dotted module paths
|
|
|
|
# (which don't cause this problem) instead of file paths (which
|
|
|
|
# do), but in the case of a directory in the cwd, which would
|
|
|
|
# be equally valid if considered as a top-level module or as a
|
|
|
|
# directory path, unittest unfortunately prefers the latter.
|
|
|
|
|
|
|
|
top_level = label_as_path
|
|
|
|
while True:
|
|
|
|
init_py = os.path.join(top_level, '__init__.py')
|
|
|
|
if os.path.exists(init_py):
|
|
|
|
try_next = os.path.dirname(top_level)
|
|
|
|
if try_next == top_level:
|
|
|
|
# __init__.py all the way down? give up.
|
|
|
|
break
|
|
|
|
top_level = try_next
|
|
|
|
continue
|
|
|
|
break
|
|
|
|
kwargs['top_level_dir'] = top_level
|
|
|
|
|
2013-12-17 00:04:28 +08:00
|
|
|
if not (tests and tests.countTestCases()) and is_discoverable(label):
|
|
|
|
# Try discovery if path is a package or directory
|
2013-05-11 11:08:45 +08:00
|
|
|
tests = self.test_loader.discover(start_dir=label, **kwargs)
|
|
|
|
|
2013-12-17 00:04:28 +08:00
|
|
|
# Make unittest forget the top-level dir it calculated from this
|
2013-05-11 11:08:45 +08:00
|
|
|
# run, to support running tests from two different top-levels.
|
|
|
|
self.test_loader._top_level_dir = None
|
|
|
|
|
|
|
|
suite.addTests(tests)
|
|
|
|
|
|
|
|
for test in extra_tests:
|
|
|
|
suite.addTest(test)
|
|
|
|
|
2015-11-07 21:57:56 +08:00
|
|
|
if self.tags or self.exclude_tags:
|
2018-02-09 21:09:52 +08:00
|
|
|
if self.verbosity >= 2:
|
|
|
|
if self.tags:
|
|
|
|
print('Including test tag(s): %s.' % ', '.join(sorted(self.tags)))
|
|
|
|
if self.exclude_tags:
|
|
|
|
print('Excluding test tag(s): %s.' % ', '.join(sorted(self.exclude_tags)))
|
2015-11-07 21:57:56 +08:00
|
|
|
suite = filter_tests_by_tags(suite, self.tags, self.exclude_tags)
|
2015-02-06 03:31:02 +08:00
|
|
|
suite = reorder_suite(suite, self.reorder_by, self.reverse)
|
|
|
|
|
|
|
|
if self.parallel > 1:
|
2015-09-10 20:06:06 +08:00
|
|
|
parallel_suite = self.parallel_test_suite(suite, self.parallel, self.failfast)
|
|
|
|
|
|
|
|
# Since tests are distributed across processes on a per-TestCase
|
|
|
|
# basis, there's no need for more processes than TestCases.
|
|
|
|
parallel_units = len(parallel_suite.subsuites)
|
2018-01-04 02:24:02 +08:00
|
|
|
self.parallel = min(self.parallel, parallel_units)
|
2015-09-10 20:06:06 +08:00
|
|
|
|
|
|
|
# If there's only one TestCase, parallelization isn't needed.
|
|
|
|
if self.parallel > 1:
|
|
|
|
suite = parallel_suite
|
2015-02-06 03:31:02 +08:00
|
|
|
|
|
|
|
return suite
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
def setup_databases(self, **kwargs):
|
2016-07-03 06:20:14 +08:00
|
|
|
return _setup_databases(
|
2015-01-11 06:52:59 +08:00
|
|
|
self.verbosity, self.interactive, self.keepdb, self.debug_sql,
|
2015-02-10 05:00:09 +08:00
|
|
|
self.parallel, **kwargs
|
2015-01-11 06:52:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def get_resultclass(self):
|
|
|
|
return DebugSQLTextTestResult if self.debug_sql else None
|
2013-05-11 11:08:45 +08:00
|
|
|
|
2016-08-05 01:26:21 +08:00
|
|
|
def get_test_runner_kwargs(self):
|
2017-01-24 08:13:49 +08:00
|
|
|
return {
|
|
|
|
'failfast': self.failfast,
|
|
|
|
'resultclass': self.get_resultclass(),
|
|
|
|
'verbosity': self.verbosity,
|
|
|
|
}
|
2016-08-05 01:26:21 +08:00
|
|
|
|
2016-03-18 22:24:13 +08:00
|
|
|
def run_checks(self):
|
|
|
|
# Checks are run after database creation since some checks require
|
|
|
|
# database access.
|
|
|
|
call_command('check', verbosity=self.verbosity)
|
|
|
|
|
2016-08-05 01:26:21 +08:00
|
|
|
def run_suite(self, suite, **kwargs):
|
|
|
|
kwargs = self.get_test_runner_kwargs()
|
|
|
|
runner = self.test_runner(**kwargs)
|
|
|
|
return runner.run(suite)
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
def teardown_databases(self, old_config, **kwargs):
|
2017-01-25 04:37:33 +08:00
|
|
|
"""Destroy all the non-mirror databases."""
|
2016-07-03 06:20:14 +08:00
|
|
|
_teardown_databases(
|
|
|
|
old_config,
|
|
|
|
verbosity=self.verbosity,
|
|
|
|
parallel=self.parallel,
|
|
|
|
keepdb=self.keepdb,
|
|
|
|
)
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
def teardown_test_environment(self, **kwargs):
|
|
|
|
unittest.removeHandler()
|
|
|
|
teardown_test_environment()
|
|
|
|
|
|
|
|
def suite_result(self, suite, result, **kwargs):
|
|
|
|
return len(result.failures) + len(result.errors)
|
|
|
|
|
2018-07-12 12:14:24 +08:00
|
|
|
def _get_databases(self, suite):
|
|
|
|
databases = set()
|
|
|
|
for test in suite:
|
|
|
|
if isinstance(test, unittest.TestCase):
|
|
|
|
test_databases = getattr(test, 'databases', None)
|
|
|
|
if test_databases == '__all__':
|
|
|
|
return set(connections)
|
|
|
|
if test_databases:
|
|
|
|
databases.update(test_databases)
|
|
|
|
else:
|
|
|
|
databases.update(self._get_databases(test))
|
|
|
|
return databases
|
|
|
|
|
|
|
|
def get_databases(self, suite):
|
|
|
|
databases = self._get_databases(suite)
|
|
|
|
if self.verbosity >= 2:
|
|
|
|
unused_databases = [alias for alias in connections if alias not in databases]
|
|
|
|
if unused_databases:
|
|
|
|
print('Skipping setup of unused database(s): %s.' % ', '.join(sorted(unused_databases)))
|
|
|
|
return databases
|
|
|
|
|
2013-05-11 11:08:45 +08:00
|
|
|
def run_tests(self, test_labels, extra_tests=None, **kwargs):
|
|
|
|
"""
|
|
|
|
Run the unit tests for all the test labels in the provided list.
|
|
|
|
|
|
|
|
Test labels should be dotted Python paths to test modules, test
|
|
|
|
classes, or test methods.
|
|
|
|
|
|
|
|
A list of 'extra' tests may also be provided; these tests
|
|
|
|
will be added to the test suite.
|
|
|
|
|
2017-01-25 04:37:33 +08:00
|
|
|
Return the number of tests that failed.
|
2013-05-11 11:08:45 +08:00
|
|
|
"""
|
|
|
|
self.setup_test_environment()
|
|
|
|
suite = self.build_suite(test_labels, extra_tests)
|
2018-07-12 12:14:24 +08:00
|
|
|
databases = self.get_databases(suite)
|
|
|
|
old_config = self.setup_databases(aliases=databases)
|
2018-10-19 07:04:28 +08:00
|
|
|
run_failed = False
|
|
|
|
try:
|
|
|
|
self.run_checks()
|
|
|
|
result = self.run_suite(suite)
|
|
|
|
except Exception:
|
|
|
|
run_failed = True
|
|
|
|
raise
|
|
|
|
finally:
|
|
|
|
try:
|
|
|
|
self.teardown_databases(old_config)
|
|
|
|
self.teardown_test_environment()
|
|
|
|
except Exception:
|
|
|
|
# Silence teardown exceptions if an exception was raised during
|
|
|
|
# runs to avoid shadowing it.
|
|
|
|
if not run_failed:
|
|
|
|
raise
|
2013-05-11 11:08:45 +08:00
|
|
|
return self.suite_result(suite, result)
|
|
|
|
|
|
|
|
|
2013-12-17 00:04:28 +08:00
|
|
|
def is_discoverable(label):
|
|
|
|
"""
|
2018-10-09 21:26:07 +08:00
|
|
|
Check if a test label points to a Python package or file directory.
|
2014-01-01 01:41:34 +08:00
|
|
|
|
|
|
|
Relative labels like "." and ".." are seen as directories.
|
2013-12-17 00:04:28 +08:00
|
|
|
"""
|
|
|
|
try:
|
|
|
|
mod = import_module(label)
|
2014-01-01 01:41:34 +08:00
|
|
|
except (ImportError, TypeError):
|
2013-12-17 00:04:28 +08:00
|
|
|
pass
|
|
|
|
else:
|
|
|
|
return hasattr(mod, '__path__')
|
|
|
|
|
|
|
|
return os.path.isdir(os.path.abspath(label))
|
|
|
|
|
|
|
|
|
2014-11-23 00:59:05 +08:00
|
|
|
def reorder_suite(suite, classes, reverse=False):
|
2013-05-11 11:08:45 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Reorder a test suite by test type.
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
`classes` is a sequence of types
|
|
|
|
|
|
|
|
All tests of type classes[0] are placed first, then tests of type
|
|
|
|
classes[1], etc. Tests with no match in classes are placed last.
|
2014-11-23 00:59:05 +08:00
|
|
|
|
2017-01-25 04:37:33 +08:00
|
|
|
If `reverse` is True, sort tests within classes in opposite order but
|
|
|
|
don't reverse test classes.
|
2013-05-11 11:08:45 +08:00
|
|
|
"""
|
|
|
|
class_count = len(classes)
|
2013-09-10 21:49:39 +08:00
|
|
|
suite_class = type(suite)
|
2014-12-14 20:37:26 +08:00
|
|
|
bins = [OrderedSet() for i in range(class_count + 1)]
|
2015-02-06 03:31:02 +08:00
|
|
|
partition_suite_by_type(suite, classes, bins, reverse=reverse)
|
2014-12-14 20:37:26 +08:00
|
|
|
reordered_suite = suite_class()
|
|
|
|
for i in range(class_count + 1):
|
|
|
|
reordered_suite.addTests(bins[i])
|
2018-12-06 04:21:09 +08:00
|
|
|
return reordered_suite
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
def partition_suite_by_type(suite, classes, bins, reverse=False):
|
2013-05-11 11:08:45 +08:00
|
|
|
"""
|
2017-01-25 04:37:33 +08:00
|
|
|
Partition a test suite by test type. Also prevent duplicated tests.
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
classes is a sequence of types
|
|
|
|
bins is a sequence of TestSuites, one more than classes
|
2014-11-23 00:59:05 +08:00
|
|
|
reverse changes the ordering of tests within bins
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
Tests of type classes[i] are added to bins[i],
|
|
|
|
tests with no match found in classes are place in bins[-1]
|
|
|
|
"""
|
2013-09-10 21:49:39 +08:00
|
|
|
suite_class = type(suite)
|
2014-11-23 00:59:05 +08:00
|
|
|
if reverse:
|
|
|
|
suite = reversed(tuple(suite))
|
2013-05-11 11:08:45 +08:00
|
|
|
for test in suite:
|
2013-09-10 21:49:39 +08:00
|
|
|
if isinstance(test, suite_class):
|
2015-02-06 03:31:02 +08:00
|
|
|
partition_suite_by_type(test, classes, bins, reverse=reverse)
|
2013-05-11 11:08:45 +08:00
|
|
|
else:
|
|
|
|
for i in range(len(classes)):
|
|
|
|
if isinstance(test, classes[i]):
|
2014-12-14 20:37:26 +08:00
|
|
|
bins[i].add(test)
|
2013-05-11 11:08:45 +08:00
|
|
|
break
|
|
|
|
else:
|
2014-12-14 20:37:26 +08:00
|
|
|
bins[-1].add(test)
|
2013-05-11 11:08:45 +08:00
|
|
|
|
|
|
|
|
2015-02-06 03:31:02 +08:00
|
|
|
def partition_suite_by_case(suite):
|
2017-01-25 04:37:33 +08:00
|
|
|
"""Partition a test suite by test case, preserving the order of tests."""
|
2015-02-06 03:31:02 +08:00
|
|
|
groups = []
|
|
|
|
suite_class = type(suite)
|
|
|
|
for test_type, test_group in itertools.groupby(suite, type):
|
|
|
|
if issubclass(test_type, unittest.TestCase):
|
|
|
|
groups.append(suite_class(test_group))
|
|
|
|
else:
|
|
|
|
for item in test_group:
|
|
|
|
groups.extend(partition_suite_by_case(item))
|
|
|
|
return groups
|
|
|
|
|
|
|
|
|
2015-11-07 21:57:56 +08:00
|
|
|
def filter_tests_by_tags(suite, tags, exclude_tags):
|
|
|
|
suite_class = type(suite)
|
|
|
|
filtered_suite = suite_class()
|
|
|
|
|
|
|
|
for test in suite:
|
|
|
|
if isinstance(test, suite_class):
|
|
|
|
filtered_suite.addTests(filter_tests_by_tags(test, tags, exclude_tags))
|
|
|
|
else:
|
|
|
|
test_tags = set(getattr(test, 'tags', set()))
|
|
|
|
test_fn_name = getattr(test, '_testMethodName', str(test))
|
|
|
|
test_fn = getattr(test, test_fn_name, test)
|
|
|
|
test_fn_tags = set(getattr(test_fn, 'tags', set()))
|
|
|
|
all_tags = test_tags.union(test_fn_tags)
|
|
|
|
matched_tags = all_tags.intersection(tags)
|
|
|
|
if (matched_tags or not tags) and not all_tags.intersection(exclude_tags):
|
|
|
|
filtered_suite.addTest(test)
|
|
|
|
|
|
|
|
return filtered_suite
|