aboutsummaryrefslogtreecommitdiffstats
path: root/meta/lib/oeqa/utils/decorators.py
blob: a0d94e6d248c117c982f19f0eb0f8f5edad84d67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# Copyright (C) 2013 Intel Corporation
#
# Released under the MIT license (see COPYING.MIT)

# Some custom decorators that can be used by unittests
# Most useful is skipUnlessPassed which can be used for
# creating dependecies between two test methods.

from oeqa.oetest import *
import logging
import sys

class skipIfFailure(object):

    def __init__(self,testcase):
        self.testcase = testcase

    def __call__(self,f):
        def wrapped_f(*args):
            if self.testcase in (oeTest.testFailures or oeTest.testErrors):
                raise unittest.SkipTest("Testcase dependency not met: %s" % self.testcase)
            return f(*args)
        wrapped_f.__name__ = f.__name__
        return wrapped_f

class skipIfSkipped(object):

    def __init__(self,testcase):
        self.testcase = testcase

    def __call__(self,f):
        def wrapped_f(*args):
            if self.testcase in oeTest.testSkipped:
                raise unittest.SkipTest("Testcase dependency not met: %s" % self.testcase)
            return f(*args)
        wrapped_f.__name__ = f.__name__
        return wrapped_f

class skipUnlessPassed(object):

    def __init__(self,testcase):
        self.testcase = testcase

    def __call__(self,f):
        def wrapped_f(*args):
            if self.testcase in oeTest.testSkipped or \
                    self.testcase in  oeTest.testFailures or \
                    self.testcase in oeTest.testErrors:
                raise unittest.SkipTest("Testcase dependency not met: %s" % self.testcase)
            return f(*args)
        wrapped_f.__name__ = f.__name__
        return wrapped_f

class testcase(object):

    def __init__(self, test_case):
        self.test_case = test_case

    def __call__(self, func):
	def wrapped_f(*args):
		return func(*args)
	wrapped_f.test_case = self.test_case
	return wrapped_f

def LogResults(original_class):
    orig_method = original_class.run

    #rewrite the run method of unittest.TestCase to add testcase logging
    def run(self, result, *args, **kws):
        orig_method(self, result, *args, **kws)
	passed = True
	testMethod = getattr(self, self._testMethodName)

	#if test case is decorated then use it's number, else use it's name
	try:
		test_case = testMethod.test_case
	except AttributeError:
		test_case = self._testMethodName

	#create custom logging level for filtering.
	custom_log_level = 100
	logging.addLevelName(custom_log_level, 'RESULTS')
	caller = os.path.basename(sys.argv[0])

	def results(self, message, *args, **kws):
	    if self.isEnabledFor(custom_log_level):
		self.log(custom_log_level, message, *args, **kws)
	logging.Logger.results = results

	logging.basicConfig(filename=os.path.join(os.getcwd(),'results-'+caller+'.log'),
                            filemode='w',
                            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
                            datefmt='%H:%M:%S',
                            level=custom_log_level)
	local_log = logging.getLogger(caller)

	#check status of tests and record it
        for (name, msg) in result.errors:
                if self._testMethodName == str(name).split(' ')[0]:
			local_log.results("Testcase "+str(test_case)+": ERROR")
			local_log.results("Testcase "+str(test_case)+":\n"+msg)
			passed = False
        for (name, msg) in result.failures:
                if self._testMethodName == str(name).split(' ')[0]:
			local_log.results("Testcase "+str(test_case)+": FAILED")
			local_log.results("Testcase "+str(test_case)+":\n"+msg)
			passed = False
        for (name, msg) in result.skipped:
                if self._testMethodName == str(name).split(' ')[0]:
			local_log.results("Testcase "+str(test_case)+": SKIPPED")
			passed = False
	if passed:
			local_log.results("Testcase "+str(test_case)+": PASSED")

    original_class.run = run
    return original_class