/runtests.py

https://gitlab.com/asmjahid/opps · Python · 91 lines · 38 code · 11 blank · 42 comment · 14 complexity · 9e04f265ea0638829bfd6a1f36193fd8 MD5 · raw file

  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. # No logging
  27. logging.disable(logging.CRITICAL)
  28. # Warnings: ignore some uninteresting ones but raise an exception on a
  29. # DeprecationWarnings.
  30. warnings.simplefilter('default')
  31. warnings.filterwarnings('error', category=DeprecationWarning)
  32. for category in [PendingDeprecationWarning, UserWarning, ImportWarning]:
  33. warnings.filterwarnings('ignore', category=category)
  34. def run_tests(verbosity, *test_args):
  35. from django_nose import NoseTestSuiteRunner
  36. test_runner = NoseTestSuiteRunner(verbosity=verbosity)
  37. if not test_args:
  38. test_args = ['tests']
  39. num_failures = test_runner.run_tests(test_args)
  40. if num_failures:
  41. sys.exit(num_failures)
  42. if __name__ == '__main__':
  43. args = sys.argv[1:]
  44. verbosity = 1
  45. if not args:
  46. # If run with no args, try and run the testsuite as fast as possible.
  47. # That means across all cores and with no high-falutin' plugins.
  48. import multiprocessing
  49. try:
  50. num_cores = multiprocessing.cpu_count()
  51. except NotImplementedError:
  52. num_cores = 4 # Guess
  53. args = ['--nocapture', '--stop', '--processes=%s' % num_cores]
  54. else:
  55. # Some args/options specified. Check to see if any nose options have
  56. # been specified. If they have, then don't set any
  57. has_options = any(map(lambda x: x.startswith('--'), args))
  58. if not has_options:
  59. # Default options:
  60. # --stop Abort on first error/failure
  61. # --nocapture Don't capture STDOUT
  62. args.extend(['--nocapture', '--stop'])
  63. else:
  64. # Remove options as nose will pick these up from sys.argv
  65. for arg in args:
  66. if arg.startswith('--verbosity'):
  67. verbosity = int(arg[-1])
  68. args = [arg for arg in args if not arg.startswith('-')]
  69. configure()
  70. run_tests(verbosity, *args)