Development of an internal social media platform with personalised dashboards for students
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.1KB

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