我有一个功能,如果测试功能成功,应该报告。
但是,如果测试函数内部有异常,我不想报告。
我尝试使用 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()
PS 我不能使用装饰器,因为我使用的是夹具。