| 1 |
import sys |
| 2 |
|
| 3 |
from django.conf import settings |
| 4 |
from django.core.management.base import BaseCommand |
| 5 |
from django.core.management.utils import get_command_line_option |
| 6 |
from django.test.runner import get_max_test_processes |
| 7 |
from django.test.utils import NullTimeKeeper, TimeKeeper, get_runner |
| 8 |
|
| 9 |
|
| 10 |
class Command(BaseCommand): |
| 11 |
help = "Discover and run tests in the specified modules or the current directory." |
| 12 |
|
| 13 |
# DiscoverRunner runs the checks after databases are set up. |
| 14 |
requires_system_checks = [] |
| 15 |
test_runner = None |
| 16 |
|
| 17 |
def run_from_argv(self, argv): |
| 18 |
""" |
| 19 |
Pre-parse the command line to extract the value of the --testrunner |
| 20 |
option. This allows a test runner to define additional command line |
| 21 |
arguments. |
| 22 |
""" |
| 23 |
self.test_runner = get_command_line_option(argv, "--testrunner") |
| 24 |
super().run_from_argv(argv) |
| 25 |
|
| 26 |
def add_arguments(self, parser): |
| 27 |
parser.add_argument( |
| 28 |
"args", |
| 29 |
metavar="test_label", |
| 30 |
nargs="*", |
| 31 |
help=( |
| 32 |
"Module paths to test; can be modulename, modulename.TestCase or " |
| 33 |
"modulename.TestCase.test_method" |
| 34 |
), |
| 35 |
) |
| 36 |
parser.add_argument( |
| 37 |
"--noinput", |
| 38 |
"--no-input", |
| 39 |
action="store_false", |
| 40 |
dest="interactive", |
| 41 |
help="Tells Django to NOT prompt the user for input of any kind.", |
| 42 |
) |
| 43 |
parser.add_argument( |
| 44 |
"--testrunner", |
| 45 |
help="Tells Django to use specified test runner class instead of " |
| 46 |
"the one specified by the TEST_RUNNER setting.", |
| 47 |
) |
| 48 |
|
| 49 |
test_runner_class = get_runner(settings, self.test_runner) |
| 50 |
|
| 51 |
if hasattr(test_runner_class, "add_arguments"): |
| 52 |
test_runner_class.add_arguments(parser) |
| 53 |
|
| 54 |
def handle(self, *test_labels, **options): |
| 55 |
TestRunner = get_runner(settings, options["testrunner"]) |
| 56 |
|
| 57 |
time_keeper = TimeKeeper() if options.get("timing", False) else NullTimeKeeper() |
| 58 |
parallel = options.get("parallel") |
| 59 |
if parallel == "auto": |
| 60 |
options["parallel"] = get_max_test_processes() |
| 61 |
test_runner = TestRunner(**options) |
| 62 |
with time_keeper.timed("Total run"): |
| 63 |
failures = test_runner.run_tests(test_labels) |
| 64 |
time_keeper.print_results() |
| 65 |
if failures: |
| 66 |
sys.exit(1) |