![](/img/trans.png)
[英]Pytest - how to link a specific setup and teardown functions to a specific test function?
[英]How to do a teardown of pytest intermediate results on test fail?
关于如何在测试之间设置和共享 pytest 夹具有足够的信息。
但是,如果测试会创建一些远程资源然后失败怎么办? 如何使 pytest 在测试开始时清理那些作为夹具不存在的资源?
您可以在 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.