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.

runtests.py 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #!/usr/bin/env python
  2. """
  3. Custom test runner
  4. If args or options, we run the testsuite as quickly as possible.
  5. If args but no options, we default to using the spec plugin and aborting on
  6. first error/failure.
  7. If options, we ignore defaults and pass options onto Nose.
  8. Examples:
  9. Run all tests (as fast as possible)
  10. $ ./runtests.py
  11. Run all unit tests (using spec output)
  12. $ ./runtests.py tests/unit
  13. Run all checkout unit tests (using spec output)
  14. $ ./runtests.py tests/unit/checkout
  15. Run all tests relating to shipping
  16. $ ./runtests.py --attr=shipping
  17. Re-run failing tests (needs to be run twice to first build the index)
  18. $ ./runtests.py ... --failed
  19. Drop into pdb when a test fails
  20. $ ./runtests.py ... --pdb-failures
  21. """
  22. import sys
  23. import logging
  24. import warnings
  25. from tests.config import configure
  26. from django.utils.six.moves import map
  27. # No logging
  28. logging.disable(logging.CRITICAL)
  29. def run_tests(verbosity, *test_args):
  30. from django_nose import NoseTestSuiteRunner
  31. test_runner = NoseTestSuiteRunner(verbosity=verbosity)
  32. if not test_args:
  33. test_args = ['tests']
  34. num_failures = test_runner.run_tests(test_args)
  35. if num_failures:
  36. sys.exit(num_failures)
  37. if __name__ == '__main__':
  38. args = sys.argv[1:]
  39. verbosity = 1
  40. if not args:
  41. # If run with no args, try and run the testsuite as fast as possible.
  42. # That means across all cores and with no high-falutin' plugins.
  43. args = ['--nocapture', '--stop', '--processes=-1']
  44. else:
  45. # Some args/options specified. Check to see if any nose options have
  46. # been specified. If they have, then don't set any
  47. has_options = any(map(lambda x: x.startswith('--'), args))
  48. if not has_options:
  49. # Default options:
  50. # --stop Abort on first error/failure
  51. # --nocapture Don't capture STDOUT
  52. args.extend(['--nocapture', '--stop'])
  53. else:
  54. # Remove options as nose will pick these up from sys.argv
  55. for arg in args:
  56. if arg.startswith('--verbosity'):
  57. verbosity = int(arg[-1])
  58. args = [arg for arg in args if not arg.startswith('-')]
  59. configure()
  60. with warnings.catch_warnings():
  61. # The warnings module in default configuration will never cause tests
  62. # to fail, as it never raises an exception. We alter that behaviour by
  63. # turning DeprecationWarnings into exceptions, but exclude warnings
  64. # triggered by third-party libs. Note: The context manager is not thread
  65. # safe. Behaviour with multiple threads is undefined.
  66. warnings.filterwarnings('error', category=DeprecationWarning)
  67. warnings.filterwarnings('error', category=RuntimeWarning)
  68. libs = r'(sorl\.thumbnail.*|bs4.*|webtest.*)'
  69. warnings.filterwarnings(
  70. 'ignore', r'.*', DeprecationWarning, libs)
  71. run_tests(verbosity, *args)