2016-11-08 45 views
0

我想找到一個乾淨的方式來執行py.test的setup_class方法失敗時執行一些代碼。給出以下示例:pytest如何對安裝失敗採取行動

class TestClass: 

    @classmethod 
    def setup_class(self): 
     print("I am performing setup actions!") 

    def test_one(self): 
     x = "this" 
     assert 'h' in x 

    def setup_cleanup(self): 
     print("I want to perfrom some cleanup action!") 

如何在安裝程序引發異常時通過py.test觸發setup_cleanup方法?

回答

0

可以在setup_class方法使用這種裝飾捕獲的異常:

def is_failed_decorator(func): 
    def wrapper(*args, **kwargs): 
     try: 
      func(*args, **kwargs) 
     except AssertionError: 
      # Your code here. 
      raise 
    return wrapper 

@is_failed_decorator 
@classmethod 
def setup_class(self):