1

テスト関数が成功した場合、レポートを実行する必要がある関数があります。

ただし、テスト関数内で例外が発生した場合は、レポートを行いたくありません。

私は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 フィクスチャを使用しているため、デコレータは使用できません。

4

0 に答える 0