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