__init__.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. # -*- coding: utf-8 -*-
  2. """
  3. plan.testsuite
  4. ~~~~~~~~~~~~~~
  5. Tests Plan itself.
  6. :copyright: (c) 2014 by Shipeng Feng.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. import sys
  10. import pkgutil
  11. import unittest
  12. from plan._compat import PY2, string_types
  13. class BaseTestCase(unittest.TestCase):
  14. """Baseclass for all the tests that Plan uses. We use this
  15. BaseTestCase for code style consistency.
  16. """
  17. def setup(self):
  18. pass
  19. def teardown(self):
  20. pass
  21. def setUp(self):
  22. self.setup()
  23. def tearDown(self):
  24. unittest.TestCase.tearDown(self)
  25. self.teardown()
  26. def assert_equal(self, first, second):
  27. return self.assertEqual(first, second)
  28. def assert_true(self, expr, msg=None):
  29. self.assertTrue(expr, msg)
  30. def assert_false(self, expr, msg=None):
  31. self.assertFalse(expr, msg)
  32. def assert_raises(self, exception, callable=None, *args, **kwargs):
  33. self.assertRaises(exception, callable, *args, **kwargs)
  34. def assert_in(self, first, second):
  35. self.assertIn(first, second)
  36. def assert_not_in(self, first, second):
  37. self.assertNotIn(first, second)
  38. def assert_isinstance(self, obj, cls):
  39. self.assertIsInstance(obj, cls)
  40. if sys.version_info[:2] == (2, 6):
  41. def assertIn(self, x, y):
  42. assert x in y, "%r not found in %r" % (x, y)
  43. def assertNotIn(self, x, y):
  44. assert x not in y, "%r unexpectedly in %r" % (x, y)
  45. def assertIsInstance(self, x, y):
  46. assert isinstance(x, y), "not isinstance(%r, %r)" % (x, y)
  47. def import_string(import_name):
  48. """Import a module based on a string.
  49. :param import_name: the dotted name for the object to import.
  50. :return: imported object
  51. """
  52. assert isinstance(import_name, string_types)
  53. if '.' in import_name:
  54. module, obj = import_name.rsplit('.', 1)
  55. else:
  56. return __import__(import_name)
  57. if PY2 and isinstance(obj, unicode):
  58. obj = obj.encode('utf-8')
  59. return getattr(__import__(module, None, None, [obj]), obj)
  60. def find_modules(import_name):
  61. """Find all modules under one package.
  62. """
  63. module = import_string(import_name)
  64. path = getattr(module, '__path__', None)
  65. if path is None:
  66. raise ValueError('%s is not a package' % import_name)
  67. basename = module.__name__ + '.'
  68. for importer, modname, ispkg in pkgutil.iter_modules(path):
  69. modname = basename + modname
  70. if not ispkg:
  71. yield modname
  72. def iter_suites():
  73. """Generator for all testsuites."""
  74. for module in find_modules(__name__):
  75. mod = import_string(module)
  76. if hasattr(mod, 'suite'):
  77. yield mod.suite()
  78. def suite():
  79. """A testsuite that has all the Plan tests.
  80. """
  81. suite = unittest.TestSuite()
  82. for module_suite in iter_suites():
  83. suite.addTest(module_suite)
  84. return suite
  85. def main():
  86. """Runs the testsuite."""
  87. unittest.main(__name__, defaultTest='suite')