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 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. import multiprocessing
  44. try:
  45. num_cores = multiprocessing.cpu_count()
  46. except NotImplementedError:
  47. num_cores = 4 # Guess
  48. args = ['--nocapture', '--stop', '--processes=%s' % num_cores]
  49. else:
  50. # Some args/options specified. Check to see if any nose options have
  51. # been specified. If they have, then don't set any
  52. has_options = any(map(lambda x: x.startswith('--'), args))
  53. if not has_options:
  54. # Default options:
  55. # --stop Abort on first error/failure
  56. # --nocapture Don't capture STDOUT
  57. args.extend(['--nocapture', '--stop'])
  58. else:
  59. # Remove options as nose will pick these up from sys.argv
  60. for arg in args:
  61. if arg.startswith('--verbosity'):
  62. verbosity = int(arg[-1])
  63. args = [arg for arg in args if not arg.startswith('-')]
  64. configure()
  65. with warnings.catch_warnings():
  66. # The warnings module in default configuration will never cause tests
  67. # to fail, as it never raises an exception. We alter that behaviour by
  68. # turning DeprecationWarnings into exceptions, but exclude warnings
  69. # triggered by third-party libs. Note: The context manager is not thread
  70. # safe. Behaviour with multiple threads is undefined.
  71. warnings.filterwarnings('error', category=DeprecationWarning)
  72. warnings.filterwarnings('error', category=RuntimeWarning)
  73. libs = r'(sorl\.thumbnail.*|bs4.*|webtest.*)'
  74. warnings.filterwarnings(
  75. 'ignore', r'.*', DeprecationWarning, libs)
  76. run_tests(verbosity, *args)