You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.py 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import sys
  2. from django.conf import settings
  3. from django.core.management.base import BaseCommand
  4. from django.core.management.utils import get_command_line_option
  5. from django.test.utils import get_runner
  6. class Command(BaseCommand):
  7. help = 'Discover and run tests in the specified modules or the current directory.'
  8. # DiscoverRunner runs the checks after databases are set up.
  9. requires_system_checks = False
  10. test_runner = None
  11. def run_from_argv(self, argv):
  12. """
  13. Pre-parse the command line to extract the value of the --testrunner
  14. option. This allows a test runner to define additional command line
  15. arguments.
  16. """
  17. self.test_runner = get_command_line_option(argv, '--testrunner')
  18. super().run_from_argv(argv)
  19. def add_arguments(self, parser):
  20. parser.add_argument(
  21. 'args', metavar='test_label', nargs='*',
  22. help='Module paths to test; can be modulename, modulename.TestCase or modulename.TestCase.test_method'
  23. )
  24. parser.add_argument(
  25. '--noinput', '--no-input', action='store_false', dest='interactive',
  26. help='Tells Django to NOT prompt the user for input of any kind.',
  27. )
  28. parser.add_argument(
  29. '--failfast', action='store_true',
  30. help='Tells Django to stop running the test suite after first failed test.',
  31. )
  32. parser.add_argument(
  33. '--testrunner',
  34. help='Tells Django to use specified test runner class instead of '
  35. 'the one specified by the TEST_RUNNER setting.',
  36. )
  37. test_runner_class = get_runner(settings, self.test_runner)
  38. if hasattr(test_runner_class, 'add_arguments'):
  39. test_runner_class.add_arguments(parser)
  40. def handle(self, *test_labels, **options):
  41. TestRunner = get_runner(settings, options['testrunner'])
  42. test_runner = TestRunner(**options)
  43. failures = test_runner.run_tests(test_labels)
  44. if failures:
  45. sys.exit(1)