Exception pytest:如果fixture中出现异常,则跳过addfinalizer

Exception pytest:如果fixture中出现异常,则跳过addfinalizer,exception,pytest,fixture,Exception,Pytest,Fixture,我有一个函数,应该做报告,如果测试函数成功 但,若测试函数中存在异常,我不想做报告 我尝试使用pytest.fixture、pytest.yield\u fixture,但它们都总是调用终结器。我怎么能理解,这个异常是在测试函数中提出的 test.py StatisticClass: start FStatisticClass: stop finalizer test.py竞赛: @pytest.mark.usefixtures("statistic_maker") def test_dumm

我有一个函数,应该做报告,如果测试函数成功

但,若测试函数中存在异常,我不想做报告

我尝试使用pytest.fixture、pytest.yield\u fixture,但它们都总是调用终结器。我怎么能理解,这个异常是在测试函数中提出的

test.py StatisticClass: start
FStatisticClass: stop
finalizer
test.py竞赛:

@pytest.mark.usefixtures("statistic_maker")
def test_dummy():
    raise Exception()
conftest.py的内容:

class StatisticClass():

    def __init__(self, req):
        self.req = req
        pass

    def start(self):
        print "StatisticClass: start"

    def stop(self):
        print "StatisticClass: stop"

    def if_not_exception(self):
        """
        I don't want to call this if Exception inside yield.
        Maybe, there is any info in request object?
        """
        print "finalizer" 


@pytest.yield_fixture(scope="function")
def statistic_maker(request):
    ds = StatisticClass(request)
    ds.start()
    request.addfinalizer(ds.if_not_exception) 
    yield
    ds.stop()

另外,我不能使用decorator,因为我使用fixture。

也许是使用普通python decorator的正确方法?它工作正常:)
@decorator\u statistic()def test\u dummy():引发异常()
def decorator\u statistic():def real\u decorator\u statistic(func):def wrapped(*args,**kwargs):ds=StatisticClass(“”)ds.start()尝试:ret=func(*args,**kwargs)ds.if_not_exception()异常除外,err:raise exception(err)finally:ds.stop()返回ret返回wrapped返回real_decorator_statistic
我不能使用decorator,因为我在测试函数中需要fixture(