blob: 14d7bfcd3567d789c53798d8dd60ba72d9d35f1d [file] [log] [blame]
Brad Bishop6e60e8b2018-02-01 10:27:11 -05001# Copyright (C) 2016 Intel Corporation
2# Released under the MIT license (see COPYING.MIT)
3
4from functools import wraps
Brad Bishopd5ae7d92018-06-14 09:52:03 -07005from abc import abstractmethod, ABCMeta
Brad Bishop6e60e8b2018-02-01 10:27:11 -05006
7decoratorClasses = set()
8
Brad Bishopd5ae7d92018-06-14 09:52:03 -07009def registerDecorator(cls):
10 decoratorClasses.add(cls)
11 return cls
Brad Bishop6e60e8b2018-02-01 10:27:11 -050012
Brad Bishopd5ae7d92018-06-14 09:52:03 -070013class OETestDecorator(object, metaclass=ABCMeta):
Brad Bishop6e60e8b2018-02-01 10:27:11 -050014 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
55class 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
63class 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