backports.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """backports of needed unittest2 features"""
  2. #=============================================================================
  3. # imports
  4. #=============================================================================
  5. from __future__ import with_statement
  6. # core
  7. import logging; log = logging.getLogger(__name__)
  8. import re
  9. import sys
  10. ##from warnings import warn
  11. # site
  12. # pkg
  13. from passlib.utils.compat import PY26
  14. # local
  15. __all__ = [
  16. "TestCase",
  17. "unittest",
  18. # TODO: deprecate these exports in favor of "unittest.XXX"
  19. "skip", "skipIf", "skipUnless",
  20. ]
  21. #=============================================================================
  22. # import latest unittest module available
  23. #=============================================================================
  24. try:
  25. import unittest2 as unittest
  26. except ImportError:
  27. if PY26:
  28. raise ImportError("Passlib's tests require 'unittest2' under Python 2.6 (as of Passlib 1.7)")
  29. # python 2.7 and python 3.2 both have unittest2 features (at least, the ones we use)
  30. import unittest
  31. #=============================================================================
  32. # unittest aliases
  33. #=============================================================================
  34. skip = unittest.skip
  35. skipIf = unittest.skipIf
  36. skipUnless = unittest.skipUnless
  37. SkipTest = unittest.SkipTest
  38. #=============================================================================
  39. # custom test harness
  40. #=============================================================================
  41. class TestCase(unittest.TestCase):
  42. """backports a number of unittest2 features in TestCase"""
  43. #===================================================================
  44. # backport some unittest2 names
  45. #===================================================================
  46. #---------------------------------------------------------------
  47. # backport assertRegex() alias from 3.2 to 2.7
  48. # was present in 2.7 under an alternate name
  49. #---------------------------------------------------------------
  50. if not hasattr(unittest.TestCase, "assertRegex"):
  51. assertRegex = unittest.TestCase.assertRegexpMatches
  52. if not hasattr(unittest.TestCase, "assertRaisesRegex"):
  53. assertRaisesRegex = unittest.TestCase.assertRaisesRegexp
  54. #===================================================================
  55. # eoc
  56. #===================================================================
  57. #=============================================================================
  58. # eof
  59. #=============================================================================