1
1
from __future__ import print_function
2
2
3
+
3
4
class AssertException (Exception ):
4
5
pass
5
6
7
+
6
8
def format_message (message ):
7
9
return message .replace ("\n " , "<:LF:>" )
8
10
9
11
10
12
def display (type , message , label = "" , mode = "" ):
11
- print ("\n <{0}:{1}:{2}>{3}" .format (type .upper (), mode .upper (), label , format_message (message )))
13
+ print ("\n <{0}:{1}:{2}>{3}" .format (
14
+ type .upper (), mode .upper (), label , format_message (message )))
12
15
13
16
14
17
def expect (passed = None , message = None , allow_raise = False ):
@@ -32,7 +35,8 @@ def assert_equals(actual, expected, message=None, allow_raise=False):
32
35
33
36
34
37
def assert_not_equals (actual , expected , message = None , allow_raise = False ):
35
- equals_msg = "{0} should not equal {1}" .format (repr (actual ), repr (expected ))
38
+ r_actual , r_expected = repr (actual ), repr (expected )
39
+ equals_msg = "{0} should not equal {1}" .format (r_actual , r_expected )
36
40
if message is None :
37
41
message = equals_msg
38
42
else :
@@ -43,21 +47,40 @@ def assert_not_equals(actual, expected, message=None, allow_raise=False):
43
47
44
48
def expect_error (message , function , exception = Exception ):
45
49
passed = False
46
- try : function ()
47
- except exception : passed = True
48
- except : pass
50
+ try :
51
+ function ()
52
+ except exception :
53
+ passed = True
54
+ except Exception :
55
+ pass
56
+ expect (passed , message )
57
+
58
+
59
+ def expect_no_error (message , function , exception = BaseException ):
60
+ passed = True
61
+ try :
62
+ function ()
63
+ except exception :
64
+ passed = False
65
+ except Exception :
66
+ pass
49
67
expect (passed , message )
50
68
51
69
52
70
def pass_ (): expect (True )
71
+
72
+
53
73
def fail (message ): expect (False , message )
54
74
55
75
56
- def assert_approx_equals (actual , expected , margin = 1e-9 , message = None , allow_raise = False ):
57
- equals_msg = "{0} should be close to {1} with absolute or relative margin of {2}" .format (
58
- repr (actual ), repr (expected ), repr (margin ))
59
- if message is None : message = equals_msg
60
- else : message += ": " + equals_msg
76
+ def assert_approx_equals (
77
+ actual , expected , margin = 1e-9 , message = None , allow_raise = False ):
78
+ msg = "{0} should be close to {1} with absolute or relative margin of {2}"
79
+ equals_msg = msg .format (repr (actual ), repr (expected ), repr (margin ))
80
+ if message is None :
81
+ message = equals_msg
82
+ else :
83
+ message += ": " + equals_msg
61
84
div = max (abs (actual ), abs (expected ), 1 )
62
85
expect (abs ((actual - expected ) / div ) < margin , message , allow_raise )
63
86
@@ -70,26 +93,33 @@ def describe1():
70
93
def it1():
71
94
# some test cases...
72
95
'''
96
+
97
+
73
98
def _timed_block_factory (opening_text ):
74
99
from timeit import default_timer as timer
75
100
from traceback import format_exception
76
101
from sys import exc_info
77
-
102
+
78
103
def _timed_block_decorator (s , before = None , after = None ):
79
104
display (opening_text , s )
105
+
80
106
def wrapper (func ):
81
- if callable (before ): before ()
107
+ if callable (before ):
108
+ before ()
82
109
time = timer ()
83
- try : func ()
84
- except :
110
+ try :
111
+ func ()
112
+ except Exception :
85
113
fail ('Unexpected exception raised' )
86
114
tb_str = '' .join (format_exception (* exc_info ()))
87
115
display ('ERROR' , tb_str )
88
116
display ('COMPLETEDIN' , '{:.2f}' .format ((timer () - time ) * 1000 ))
89
- if callable (after ): after ()
117
+ if callable (after ):
118
+ after ()
90
119
return wrapper
91
120
return _timed_block_decorator
92
121
122
+
93
123
describe = _timed_block_factory ('DESCRIBE' )
94
124
it = _timed_block_factory ('IT' )
95
125
@@ -102,10 +132,16 @@ def some_tests():
102
132
any code block...
103
133
Note: Timeout value can be a float.
104
134
'''
135
+
136
+
105
137
def timeout (sec ):
106
138
def wrapper (func ):
107
139
from multiprocessing import Process
108
- process = Process (target = func )
140
+ msg = 'Should not throw any exception inside timeout'
141
+
142
+ def wrapped ():
143
+ expect_no_error (msg , func )
144
+ process = Process (target = wrapped )
109
145
process .start ()
110
146
process .join (sec )
111
147
if process .is_alive ():
0 commit comments