简体   繁体   English

如何在测试失败时拆解 pytest 中间结果?

[英]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.关于如何在测试之间设置和共享 pytest 夹具有足够的信息。

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?如何使 pytest 在测试开始时清理那些作为夹具不存在的资源?

You can keep a variable in class level您可以在 class 级别保留变量

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 pytest-如何将特定的设置和拆卸功能链接到特定的测试功能? - Pytest - how to link a specific setup and teardown functions to a specific test function? 如何在“ pytest”框架的夹具中的“拆卸”中使用测试结果 - How to use test result in “teardown” in fixtures in “pytest” framework 如何在pytest中完成每次测试后完全拆解烧瓶应用程序? - How to Completely Teardown Flask App After Each Test in pytest? 使用pytest进行设置和拆卸 - Using pytest to do setup and teardown 如何使用测试正确设置和拆卸我的 pytest 类? - How do I correctly setup and teardown for my pytest class with tests? Pytest 如何在拆卸方法中获取标记参数? - Pytest how do i get the mark parameter inside a teardown method? 我如何在pytest的teardown_module()中获得模块的所有测试的结果(通过/失败) - How can i get result(Pass/Fail) of all tests of a module in teardown_module() in pytest 如何通过检查传递给pytest_runtest_teardown的Item对象来确定测试是通过还是失败? - How can I determine if a test passed or failed by examining the Item object passed to the pytest_runtest_teardown? 如何在另一个文件 test_1.py 中处理 pytest 的设置和拆卸方法中生成的 session - How to handle session which is generated in the setup and teardown methods in pytest in another file test_1.py 在 pytest 中,如何中止夹具拆卸? - In pytest, how can I abort the fixture teardown?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM