pytest如何对安装失败采取措施

时间:2016-11-08 21:14:23

标签: python testing pytest

当py.test的setup_class方法失败时,我试图找到一种执行某些代码的简洁方法。给出以下示例:

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!")

如果setup引发异常,如何让py.test触发setup_cleanup方法?

1 个答案:

答案 0 :(得分: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):