helpers.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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_new_test_class(self, name, testcase_class):
  27. """
  28. Generates a new type which inherits from the given testcase_class and unittest.TestCase
  29. """
  30. class_name = "{0}{1}".format(name, testcase_class.__name__)
  31. return type(class_name, (testcase_class, unittest.TestCase), {})
  32. def generate_test_suite_for_provider_testcase(self, provider_class, testcase_class):
  33. """
  34. Generate and return a suite of tests for a specific provider class and testcase
  35. combination
  36. """
  37. testloader = unittest.TestLoader()
  38. testnames = testloader.getTestCaseNames(testcase_class)
  39. suite = unittest.TestSuite()
  40. for name in testnames:
  41. generated_cls = self.generate_new_test_class(provider_class.__name__, testcase_class)
  42. suite.addTest(generated_cls(name, self.create_provider_instance(provider_class)))
  43. return suite
  44. def generate_test_suite_for_provider(self, provider_class):
  45. """
  46. Generate and return a suite of all available tests for a given provider class
  47. """
  48. suite = unittest.TestSuite()
  49. suites = map(lambda test_class: self.generate_test_suite_for_provider_testcase(
  50. provider_class, test_class), self.all_test_classes)
  51. map(suite.addTest, suites)
  52. return suite
  53. def generate_tests(self):
  54. """
  55. Generate and return a suite of tests for all provider and test class combinations
  56. """
  57. factory = CloudProviderFactory()
  58. provider_classes = factory.get_all_provider_classes()
  59. suite = unittest.TestSuite()
  60. suites = map(self.generate_test_suite_for_provider, provider_classes)
  61. map(suite.addTest, suites)
  62. return suite