test.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Cement testing utilities."""
  2. import unittest
  3. from tempfile import mkstemp, mkdtemp
  4. from ..core import backend, foundation
  5. from ..utils.misc import rando
  6. # shortcuts
  7. from nose import SkipTest
  8. from nose.tools import ok_ as ok
  9. from nose.tools import eq_ as eq
  10. from nose.tools import raises
  11. from nose.plugins.attrib import attr
  12. class TestApp(foundation.CementApp):
  13. """
  14. Basic CementApp for generic testing.
  15. """
  16. class Meta:
  17. label = "app-%s" % rando()[:12]
  18. config_files = []
  19. argv = []
  20. base_controller = None
  21. arguments = []
  22. exit_on_close = False
  23. class CementTestCase(unittest.TestCase):
  24. """
  25. A sub-class of unittest.TestCase.
  26. """
  27. app_class = TestApp
  28. """The test class that is used by self.make_app to create an app."""
  29. def __init__(self, *args, **kw):
  30. super(CementTestCase, self).__init__(*args, **kw)
  31. def setUp(self):
  32. """
  33. Sets up self.app with a generic TestApp(). Also resets the backend
  34. hooks and handlers so that everytime an app is created it is setup
  35. clean each time.
  36. """
  37. self.app = self.make_app()
  38. _, self.tmp_file = mkstemp()
  39. self.tmp_dir = mkdtemp()
  40. def make_app(self, *args, **kw):
  41. """
  42. Create a generic app using TestApp. Arguments and Keyword Arguments
  43. are passed to the app.
  44. """
  45. self.reset_backend()
  46. return self.app_class(*args, **kw)
  47. def reset_backend(self):
  48. """
  49. Remove all registered hooks and handlers from the backend.
  50. """
  51. # FIXME: This should not be needed anymore once we fully remove
  52. # backend_globals (Cement 3)
  53. for _handler in backend.__handlers__.copy():
  54. del backend.__handlers__[_handler]
  55. for _hook in backend.__hooks__.copy():
  56. del backend.__hooks__[_hook]
  57. def ok(self, expr, msg=None):
  58. """Shorthand for assert."""
  59. return ok(expr, msg)
  60. def eq(self, a, b, msg=None):
  61. """Shorthand for 'assert a == b, "%r != %r" % (a, b)'. """
  62. return eq(a, b, msg)
  63. # The following are for internal, Cement unit testing only
  64. @attr('core')
  65. class CementCoreTestCase(CementTestCase):
  66. pass
  67. @attr('ext')
  68. class CementExtTestCase(CementTestCase):
  69. pass