如何在测试失败时剥离pytest中间结果?

2024-04-20 00:25:38 发布

您现在位置:Python中文网/ 问答频道 /正文

关于如何在测试之间设置和共享pytestfixture,有足够的信息。你知道吗

但是如果一个测试会创建一些远程资源然后失败呢?如何使pytest清除那些在测试开始时不作为fixture存在的资源?你知道吗


Tags: 信息远程pytest资源fixturepytestfixture
1条回答
网友
1楼 · 发布于 2024-04-20 00:25:38

可以在类级别保留变量

@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()

相关问题 更多 >