|
12 | 12 | # See the License for the specific language governing permissions and |
13 | 13 | # limitations under the License. |
14 | 14 |
|
| 15 | +from typing import Any, Dict, Type |
15 | 16 |
|
16 | | -def skip(reason): |
17 | | - def __inner_skip(orig_class): |
18 | | - setattr(orig_class, "__tag_skip", True) |
19 | | - setattr(orig_class, "__tag_skip_reason", reason) |
20 | 17 |
|
21 | | - def __init__(self, *args_, **kwargs_): |
22 | | - pass |
| 18 | +class TestTag: |
| 19 | + """Central registry for managing test tag""" |
23 | 20 |
|
24 | | - # Ignore initialization of skipped modules |
25 | | - orig_class.__init__ = __init__ |
| 21 | + _registry: Dict[Type, Dict[str, Any]] = {} |
26 | 22 |
|
27 | | - return orig_class |
| 23 | + @classmethod |
| 24 | + def add(cls, test_class: Type, tag_key: str, tag_value: Any = None) -> None: |
| 25 | + """Add test tag object to a class |
28 | 26 |
|
29 | | - return __inner_skip |
| 27 | + Args: |
| 28 | + test_class: The test class to add tag to |
| 29 | + tag_key: Name of Tag object to add |
| 30 | + tag_value: Tag object to add |
| 31 | + """ |
| 32 | + if test_class not in cls._registry: |
| 33 | + cls._registry[test_class] = {} |
30 | 34 |
|
| 35 | + cls._registry[test_class][tag_key] = tag_value |
31 | 36 |
|
32 | | -def skip_if(predicate, reason): |
33 | | - def __inner_skip(orig_class): |
34 | | - setattr(orig_class, "__tag_skip", True) |
35 | | - setattr(orig_class, "__tag_skip_reason", reason) |
| 37 | + @classmethod |
| 38 | + def has(cls, test_class: Type, tag_key: str) -> bool: |
| 39 | + """Check if a class has specific tag type |
36 | 40 |
|
37 | | - def __init__(self, *args_, **kwargs_): |
38 | | - pass |
| 41 | + Args: |
| 42 | + test_class: The test class to check |
| 43 | + tag_key: Type of tag object to check for |
39 | 44 |
|
40 | | - # Ignore initialization of skipped modules |
41 | | - orig_class.__init__ = __init__ |
| 45 | + Returns: |
| 46 | + bool: True if the tag exists, False otherwise |
| 47 | + """ |
| 48 | + return test_class in cls._registry and tag_key in cls._registry[test_class] |
42 | 49 |
|
43 | | - return orig_class |
| 50 | + @classmethod |
| 51 | + def get(cls, test_class: Type, tag_key: str, default: Any = None) -> Any: |
| 52 | + """Get tag object for a class |
44 | 53 |
|
45 | | - if predicate: |
46 | | - return __inner_skip |
47 | | - else: |
48 | | - return lambda x: x |
| 54 | + Args: |
| 55 | + test_class: The test class to get tag from |
| 56 | + tag_key: Type of tag object to retrieve |
| 57 | + default: Default value to return if tag not found |
49 | 58 |
|
| 59 | + Returns: |
| 60 | + The tag object or default if not found |
| 61 | + """ |
| 62 | + return cls._registry.get(test_class, {}).get(tag_key, default) |
50 | 63 |
|
51 | | -def test_without_inference(orig_class): |
52 | | - setattr(orig_class, "__tag_test_without_inference", True) |
53 | | - return orig_class |
54 | 64 |
|
| 65 | +#################################################################### |
| 66 | +################## Add tag here ################## |
| 67 | +#################################################################### |
55 | 68 |
|
56 | | -def test_without_pt2(orig_class): |
57 | | - setattr(orig_class, "__tag_test_without_pt2", True) |
58 | | - return orig_class |
59 | 69 |
|
| 70 | +def skip(reason): |
| 71 | + """ |
| 72 | + Mark a test class to be skipped with a reason |
60 | 73 |
|
61 | | -def test_negative(expected_err): |
62 | | - def __inner_test_negative(orig_class): |
63 | | - setattr(orig_class, "__tag_test_negative", True) |
64 | | - setattr(orig_class, "__tag_expected_err", expected_err) |
| 74 | + e.g. |
| 75 | + @skip(reason="Not implemented yet") |
| 76 | + class MyTest(unittest.TestCase): # <-- This test will be skipped |
| 77 | + """ |
65 | 78 |
|
66 | | - return orig_class |
| 79 | + def decorator(cls): |
| 80 | + TestTag.add(cls, "skip", {"reason": reason}) |
| 81 | + return cls |
67 | 82 |
|
68 | | - return __inner_test_negative |
| 83 | + return decorator |
69 | 84 |
|
70 | 85 |
|
71 | | -def target(orig_class): |
72 | | - setattr(orig_class, "__tag_target", True) |
73 | | - return orig_class |
| 86 | +def skip_if(predicate, reason): |
| 87 | + """Conditionally mark a test class to be skipped with a reason""" |
| 88 | + if predicate: |
| 89 | + return skip(reason) |
| 90 | + return lambda cls: cls |
74 | 91 |
|
75 | 92 |
|
76 | | -def use_onert(orig_class): |
77 | | - """ |
78 | | - Decorator to mark a test class so that Circle models are executed |
79 | | - with the 'onert' runtime. |
| 93 | +def test_negative(expected_err): |
| 94 | + """Mark a test class as negative test case with expected error""" |
80 | 95 |
|
81 | | - Useful when the default 'circle-interpreter' cannot run the model |
82 | | - under test. |
83 | | - """ |
84 | | - setattr(orig_class, "__tag_use_onert", True) |
85 | | - return orig_class |
| 96 | + def decorator(cls): |
| 97 | + TestTag.add(cls, "test_negative", {"expected_err": expected_err}) |
| 98 | + return cls |
| 99 | + |
| 100 | + return decorator |
86 | 101 |
|
87 | 102 |
|
88 | | -def init_args(*args, **kwargs): |
89 | | - def __inner_init_args(orig_class): |
90 | | - orig_init = orig_class.__init__ |
91 | | - # Make copy of original __init__, so we can call it without recursion |
| 103 | +def target(cls): |
| 104 | + """Mark a test class as target test case""" |
| 105 | + TestTag.add(cls, "target") |
| 106 | + return cls |
92 | 107 |
|
93 | | - def __init__(self, *args_, **kwargs_): |
94 | | - args_ = (*args, *args_) |
95 | | - kwargs_ = {**kwargs, **kwargs_} |
96 | 108 |
|
97 | | - orig_init(self, *args_, **kwargs_) # Call the original __init__ |
| 109 | +def use_onert(cls): |
| 110 | + """Mark a test class to use ONERT runtime""" |
| 111 | + TestTag.add(cls, "use_onert") |
| 112 | + return cls |
98 | 113 |
|
99 | | - orig_class.__init__ = __init__ |
100 | | - return orig_class |
101 | 114 |
|
102 | | - return __inner_init_args |
| 115 | +def test_without_pt2(cls): |
| 116 | + """Mark a test class to not convert along pt2 during test execution""" |
| 117 | + TestTag.add(cls, "test_without_pt2") |
| 118 | + return cls |
103 | 119 |
|
104 | 120 |
|
105 | | -def is_tagged(cls, tag: str): |
106 | | - return hasattr(cls, f"__tag_{tag}") |
| 121 | +def test_without_inference(cls): |
| 122 | + """Mark a test class to not run inference during test execution""" |
| 123 | + TestTag.add(cls, "test_without_inference") |
| 124 | + return cls |
0 commit comments