diff options
author | Lucian Musat <georgex.l.musat@intel.com> | 2014-06-27 17:32:13 +0300 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2014-07-03 17:41:17 +0100 |
commit | ae03111791f45036e7c24378b06b997f8c7cb0a7 (patch) | |
tree | 6a82a37ec4d6a0a9d61ea4eb1892df72887fd2a6 /meta/lib/oeqa/utils/decorators.py | |
parent | 52a6d20519870103134166d91e22d21fd736195d (diff) | |
download | poky-ae03111791f45036e7c24378b06b997f8c7cb0a7.tar.gz |
Added testcase decorator to use in logging. Added class decorator LogResults that outputs test results in separate log file.
(From OE-Core rev: 7e2b73f1ccfe2968ef780fef2edfaa31c3dae853)
Signed-off-by: Lucian Musat <georgex.l.musat@intel.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/lib/oeqa/utils/decorators.py')
-rw-r--r-- | meta/lib/oeqa/utils/decorators.py | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/meta/lib/oeqa/utils/decorators.py b/meta/lib/oeqa/utils/decorators.py index b99da8d76d..a0d94e6d24 100644 --- a/meta/lib/oeqa/utils/decorators.py +++ b/meta/lib/oeqa/utils/decorators.py | |||
@@ -7,6 +7,8 @@ | |||
7 | # creating dependecies between two test methods. | 7 | # creating dependecies between two test methods. |
8 | 8 | ||
9 | from oeqa.oetest import * | 9 | from oeqa.oetest import * |
10 | import logging | ||
11 | import sys | ||
10 | 12 | ||
11 | class skipIfFailure(object): | 13 | class skipIfFailure(object): |
12 | 14 | ||
@@ -48,3 +50,67 @@ class skipUnlessPassed(object): | |||
48 | return f(*args) | 50 | return f(*args) |
49 | wrapped_f.__name__ = f.__name__ | 51 | wrapped_f.__name__ = f.__name__ |
50 | return wrapped_f | 52 | return wrapped_f |
53 | |||
54 | class testcase(object): | ||
55 | |||
56 | def __init__(self, test_case): | ||
57 | self.test_case = test_case | ||
58 | |||
59 | def __call__(self, func): | ||
60 | def wrapped_f(*args): | ||
61 | return func(*args) | ||
62 | wrapped_f.test_case = self.test_case | ||
63 | return wrapped_f | ||
64 | |||
65 | def LogResults(original_class): | ||
66 | orig_method = original_class.run | ||
67 | |||
68 | #rewrite the run method of unittest.TestCase to add testcase logging | ||
69 | def run(self, result, *args, **kws): | ||
70 | orig_method(self, result, *args, **kws) | ||
71 | passed = True | ||
72 | testMethod = getattr(self, self._testMethodName) | ||
73 | |||
74 | #if test case is decorated then use it's number, else use it's name | ||
75 | try: | ||
76 | test_case = testMethod.test_case | ||
77 | except AttributeError: | ||
78 | test_case = self._testMethodName | ||
79 | |||
80 | #create custom logging level for filtering. | ||
81 | custom_log_level = 100 | ||
82 | logging.addLevelName(custom_log_level, 'RESULTS') | ||
83 | caller = os.path.basename(sys.argv[0]) | ||
84 | |||
85 | def results(self, message, *args, **kws): | ||
86 | if self.isEnabledFor(custom_log_level): | ||
87 | self.log(custom_log_level, message, *args, **kws) | ||
88 | logging.Logger.results = results | ||
89 | |||
90 | logging.basicConfig(filename=os.path.join(os.getcwd(),'results-'+caller+'.log'), | ||
91 | filemode='w', | ||
92 | format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', | ||
93 | datefmt='%H:%M:%S', | ||
94 | level=custom_log_level) | ||
95 | local_log = logging.getLogger(caller) | ||
96 | |||
97 | #check status of tests and record it | ||
98 | for (name, msg) in result.errors: | ||
99 | if self._testMethodName == str(name).split(' ')[0]: | ||
100 | local_log.results("Testcase "+str(test_case)+": ERROR") | ||
101 | local_log.results("Testcase "+str(test_case)+":\n"+msg) | ||
102 | passed = False | ||
103 | for (name, msg) in result.failures: | ||
104 | if self._testMethodName == str(name).split(' ')[0]: | ||
105 | local_log.results("Testcase "+str(test_case)+": FAILED") | ||
106 | local_log.results("Testcase "+str(test_case)+":\n"+msg) | ||
107 | passed = False | ||
108 | for (name, msg) in result.skipped: | ||
109 | if self._testMethodName == str(name).split(' ')[0]: | ||
110 | local_log.results("Testcase "+str(test_case)+": SKIPPED") | ||
111 | passed = False | ||
112 | if passed: | ||
113 | local_log.results("Testcase "+str(test_case)+": PASSED") | ||
114 | |||
115 | original_class.run = run | ||
116 | return original_class | ||