-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainframe.py
134 lines (119 loc) · 4.93 KB
/
mainframe.py
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# -*- coding: utf8 -*-
'''
testing engine.
run by TestMainframe(), args as passed, or get from command-line
Машина за пускане на Тестове
пуска се чрез TestMainframe(), с подадени параметри или се вземат от командния ред
'''
import unittest
from .func import myTestCase4Function
# параметри от командния ред
from svd_util.config import Config
class Config( Config):
_enable_type_conversion = False
errors = False
all_cases = False
module = None
case = None
sample = None
_help = '''
errors :: ( print traceback if error)
all_cases :: show all TestCases in the choosen module
module= :: ( име на модул, от който да се заредят тестовете )
case= :: ( групата тестове, която да се пусне)
sample= :: ( 1 или няколко тест-а, които да се изпълнят от дадена група)
'''
config = Config()
class SampleBase( object):
'Базов клас за Примери'
ENGINE_ARGS = 'expect info name'.split()
def __init__( me, **kargs):
for k,v in kargs.items():
if k in me.ENGINE_ARGS:
setattr(me, k, v)
from svd_util.struct import DictAttr
class TestBase( unittest.TestCase):
'Базов тестови клас! Името на наследниците му, които ще бъдат пускани трябва да започват с Test_'
case = None
TESTOVE = []
def test_me( me): pass
def __call__( me, resulter):
for test in me.TESTOVE:
value = DictAttr( (k,v) for k,v in test.__dict__.items() if k not in SampleBase.ENGINE_ARGS)
me.case.setup( value, test.expect, test.info ).run( resulter)
############################
from svd_util.attr import issubclass
class TestMainframe( unittest.TestLoader):
def __init__( me, module =None, testcase =None, sample =None):
config.getopt()
me.module_name = module or config.module
me.testcase = testcase or config.case
me.sample = sample or config.sample
me.translator= {}
'translator = { "човешко" име : име от модула } - Опростени имена на cases '
def _importFromFile( me, name):
assert name
module = __import__( name)
for k, v in vars( module).items():
if issubclass( v, TestBase):
try:
n= v.__name__.split('_', 1)[1]
me.translator[ n.lower()] = v
except IndexError:
pass
return module
def stripTestCase( me, testove):
samples= me.sample.split(',')
gotovi_primeri= []
err= []
for s in samples:
if s == '': continue
try:
t= int( s)
try:
if t and testove[t-1] not in gotovi_primeri: #XXX A edin test >1 pyt???
gotovi_primeri.append( testove[t-1])
except IndexError:
err.append(t)
except ValueError:
for edin_test in testove:
if s == edin_test.name:
if not edin_test in gotovi_primeri:
gotovi_primeri.append( edin_test)
else:
if ( s not in err) and ( s not in [ m.name for m in testove]):
err.append(s)
if err:
print(40*'-')
for e in err: print('\nНе е намерен пример: %(e)s '%locals())
return gotovi_primeri
def groupTests( me, module):
if me.testcase and me.sample: # TestCase and Sample(s)
klas= me.translator.get( me.testcase)
klas.TESTOVE= me.stripTestCase( klas.TESTOVE)
case= unittest.TestLoader().loadTestsFromTestCase( klas)
suite= unittest.TestSuite( case)
elif me.testcase and not me.sample: # TestCase
tc= me.translator.get( me.testcase)
suite= unittest.TestLoader().loadTestsFromName( tc.__name__, module= module)
else: # All
suite= unittest.TestLoader().loadTestsFromModule( module= module)
return suite
def runTest( me, exit_on_error =True): # see ./utils.py
success = False
try:
k = me._importFromFile( me.module_name)
if config.all_cases:
print(list(me.translator.keys()))
raise SystemExit
suite = me.groupTests( k)
success = unittest.TextTestRunner( verbosity= 2).run( suite).wasSuccessful()
except Exception:
raise
if exit_on_error and not success:
raise SystemExit(not success)
return success
if __name__ == '__main__':
test = TestMainframe( module = config.module)
test.runTest()
# vim:ts=4:sw=4:expandtab