trivial.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """Tools so trivial that tracebacks should not descend into them
  2. We define the ``__unittest`` symbol in their module namespace so unittest will
  3. skip them when printing tracebacks, just as it does for their corresponding
  4. methods in ``unittest`` proper.
  5. """
  6. import re
  7. import unittest
  8. __all__ = ['ok_', 'eq_']
  9. # Use the same flag as unittest itself to prevent descent into these functions:
  10. __unittest = 1
  11. def ok_(expr, msg=None):
  12. """Shorthand for assert. Saves 3 whole characters!
  13. """
  14. if not expr:
  15. raise AssertionError(msg)
  16. def eq_(a, b, msg=None):
  17. """Shorthand for 'assert a == b, "%r != %r" % (a, b)
  18. """
  19. if not a == b:
  20. raise AssertionError(msg or "%r != %r" % (a, b))
  21. #
  22. # Expose assert* from unittest.TestCase
  23. # - give them pep8 style names
  24. #
  25. caps = re.compile('([A-Z])')
  26. def pep8(name):
  27. return caps.sub(lambda m: '_' + m.groups()[0].lower(), name)
  28. class Dummy(unittest.TestCase):
  29. def nop():
  30. pass
  31. _t = Dummy('nop')
  32. for at in [ at for at in dir(_t)
  33. if at.startswith('assert') and not '_' in at ]:
  34. pepd = pep8(at)
  35. vars()[pepd] = getattr(_t, at)
  36. __all__.append(pepd)
  37. del Dummy
  38. del _t
  39. del pep8