helpers.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. import unittest
  2. from cloudbridge.providers.factory import CloudProviderFactory
  3. class ProviderTestBase(object):
  4. """
  5. A dummy base class for Test Cases. Does not inherit from unittest.TestCase
  6. to avoid confusing test discovery by unittest and nose2. unittest.TestCase
  7. is injected as a base class by the generator, so calling the unittest constructor
  8. works correctly.
  9. """
  10. def __init__(self, methodName, provider):
  11. unittest.TestCase.__init__(self, methodName=methodName)
  12. self.provider = provider
  13. class ProviderTestCaseGenerator():
  14. """
  15. Generates test cases for all provider - testcase combinations.
  16. Detailed docs at test/__init__.py
  17. """
  18. def __init__(self, test_classes):
  19. self.all_test_classes = test_classes
  20. def create_provider_instance(self, provider_class):
  21. """
  22. Instantiate a default provider instance. All required connection settings
  23. are expected to be set as environment variables.
  24. """
  25. return provider_class({})
  26. def generate_test_suite_for_provider_testcase(self, provider_class, testcase_class):
  27. """
  28. Generate and return a suite of tests for a specific provider class and testcase
  29. combination
  30. """
  31. testloader = unittest.TestLoader()
  32. testnames = testloader.getTestCaseNames(testcase_class)
  33. suite = unittest.TestSuite()
  34. for name in testnames:
  35. generated_cls = type(
  36. "test" + provider_class.__name__ + str(testcase_class.__name__), (testcase_class, unittest.TestCase), {})
  37. suite.addTest(
  38. generated_cls(name, self.create_provider_instance(provider_class)))
  39. return suite
  40. def generate_test_suite_for_provider(self, provider_class):
  41. """
  42. Generate and return a suite of all available tests for a given provider class
  43. """
  44. suite = unittest.TestSuite()
  45. suites = map(lambda test_class: self.generate_test_suite_for_provider_testcase(
  46. provider_class, test_class), self.all_test_classes)
  47. map(suite.addTest, suites)
  48. return suite
  49. def generate_tests(self):
  50. """
  51. Generate and return a suite of tests for all provider and test class combinations
  52. """
  53. factory = CloudProviderFactory()
  54. provider_classes = factory.get_all_provider_classes()
  55. suite = unittest.TestSuite()
  56. suites = map(self.generate_test_suite_for_provider, provider_classes)
  57. map(suite.addTest, suites)
  58. return suite