pytest:如果夹具中有异常,则跳过addfinalizer

时间:2015-04-20 16:37:00

标签: exception pytest fixture

如果测试功能成功,我有一个应该报告的功能。

但是,如果测试函数中存在异常,我不想做报告。

我尝试使用pytest.fixture,pytest.yield_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()

P.S。我不能使用装饰器,因为我使用夹具。

0 个答案:

没有答案
相关问题