我編寫了一個與unittest.TestCase
非常類似的Python流控制框架:用戶創建一個從框架類派生的類,然後編寫自定義的task_*(self)
方法。該框架發現他們,並運行它們:檢測方法是否在調用之前進行了修飾
###################
# FRAMEWORK LIBRARY
###################
import functools
class SkipTask(BaseException):
pass
def skip_if(condition):
def decorator(task):
@functools.wraps(task)
def wrapper(self, *args, **kargs):
if condition(self):
raise SkipTask()
return task(self, *args, **kargs)
return wrapper
return decorator
class MyFramework(object):
def run(self):
print "Starting task"
try:
self.task()
except SkipTask:
print "Skipped task"
except Exception:
print "Failed task"
raise
else:
print "Finished task"
#############
# USER SCRIPT
#############
class MyUserClass(MyFramework):
skip_flag = True
@skip_if(lambda self: self.skip_flag)
def task(self):
print "Doing something"
if __name__ == '__main__':
MyUserClass().run()
輸出:
Starting task
Skipped task
我想改變,這樣的框架下,每當@skip_if
條件爲True
,包裝不打印"Starting task"
。 我想這一點,但它不工作:
def skip_if(condition):
def decorator(task):
print "decorating " + str(task)
task.__skip_condition = condition
return task
return decorator
class MyFramework(object):
def run(self):
try:
if self.task.__skip_condition():
print "Skipped task"
return
except AttributeError:
print str(self.task) + " is not decorated"
pass
print "Starting task"
try:
self.task()
except Exception as e:
print "Failed task: " + str(e)
raise
else:
print "Finished task"
輸出:
decorating <function task at 0x194fcd70>
<bound method MyUserClass.task of <__main__.MyUserClass object at 0x195010d0>> is not decorated
Starting task
Doing something
Finished task
爲什麼沒有任務跳過呢?
你能更具體比*「它不起作用」* - 你期望發生什麼? – jonrsharpe
@jonrsharpe:輸出應該指示測試被跳過;而是運行。 –