简体   繁体   中英

How to do a teardown of pytest intermediate results on test fail?

There is enough information on how to setup and share pytest fixtures between tests.

But what if a test would create some remote resources and then fail? How to make pytest to cleanup those resources which haven't existed as fixtures at test beginning?

You can keep a variable in class level

@pytest.mark.usefixtures("run_for_test")
class TestExample:

    __some_resource = None

    @pytest.fixture
    def run_for_test(self):
        set_up()
        yield
        if self.__some_resource:
            self.__some_resource.cleanup()

    def test_example(self):
        self.__some_resource = SomeResource()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM