__init__.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #
  2. # Copyright (C) 2016 Intel Corporation
  3. #
  4. # SPDX-License-Identifier: MIT
  5. #
  6. from functools import wraps
  7. from abc import abstractmethod, ABCMeta
  8. decoratorClasses = set()
  9. def registerDecorator(cls):
  10. decoratorClasses.add(cls)
  11. return cls
  12. class OETestDecorator(object, metaclass=ABCMeta):
  13. case = None # Reference of OETestCase decorated
  14. attrs = None # Attributes to be loaded by decorator implementation
  15. def __init__(self, *args, **kwargs):
  16. if not self.attrs:
  17. return
  18. for idx, attr in enumerate(self.attrs):
  19. if attr in kwargs:
  20. value = kwargs[attr]
  21. else:
  22. value = args[idx]
  23. setattr(self, attr, value)
  24. def __call__(self, func):
  25. @wraps(func)
  26. def wrapped_f(*args, **kwargs):
  27. self.attrs = self.attrs # XXX: Enables OETestLoader discover
  28. return func(*args, **kwargs)
  29. return wrapped_f
  30. # OETestLoader call it when is loading test cases.
  31. # XXX: Most methods would change the registry for later
  32. # processing; be aware that filtrate method needs to
  33. # run later than bind, so there could be data (in the
  34. # registry) of a cases that were filtered.
  35. def bind(self, registry, case):
  36. self.case = case
  37. self.logger = case.tc.logger
  38. self.case.decorators.append(self)
  39. # OETestRunner call this method when tries to run
  40. # the test case.
  41. def setUpDecorator(self):
  42. pass
  43. # OETestRunner call it after a test method has been
  44. # called even if the method raised an exception.
  45. def tearDownDecorator(self):
  46. pass
  47. class OETestDiscover(OETestDecorator):
  48. # OETestLoader call it after discover test cases
  49. # needs to return the cases to be run.
  50. @staticmethod
  51. def discover(registry):
  52. return registry['cases']
  53. class OETestFilter(OETestDecorator):
  54. # OETestLoader call it while loading the tests
  55. # in loadTestsFromTestCase method, it needs to
  56. # return a bool, True if needs to be filtered.
  57. # This method must consume the filter used.
  58. @abstractmethod
  59. def filtrate(self, filters):
  60. return False