asserts.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. from nose.tools import assert_true, assert_false
  2. '''
  3. Various nose.tools helpers that doesn't exists in Python 2.6 Unittest :(
  4. Must be removed with drop Python 2.6 support
  5. '''
  6. def assert_is_instance(obj, cls, msg=None):
  7. """Same as assert_true(isinstance(obj, cls)), with a nicer
  8. default message."""
  9. if not msg:
  10. msg = '{orig} is not an instance of {test}'.format(orig=type(obj), test=cls)
  11. assert_true(isinstance(obj, cls), msg=msg)
  12. def assert_is_none(obj, msg=None):
  13. """Same as assert_true(obj is None), with a nicer default message."""
  14. if not msg:
  15. msg = '{orig!r} is not None'.format(orig=obj)
  16. assert_true(obj is None, msg=msg)
  17. def assert_is_not_none(obj, msg=None):
  18. """Same as assert_false(obj is None), with a nicer default message."""
  19. if not msg:
  20. msg = '{orig!r} is None'.format(orig=obj)
  21. assert_false(obj is None, msg=msg)
  22. def assert_in(member, container, msg=None):
  23. """Just like assert_true(a in b), but with a nicer default message."""
  24. if not msg:
  25. msg = '{member!r} not found in {container!r}'.format(member=member, container=container)
  26. assert_true(member in container, msg=msg)