![](/img/trans.png)
[英]Pytest - how to link a specific setup and teardown functions to a specific test function?
[英]How can I define a setup and teardown function for a specific method or function in pytest?
import pytest
@pytest.fixture
def resource():
resource = foo()
yield resource
resource.cleanup()
def test_feature(resource):
assert bar(resource) == 27
即使没有代码重用目标,使用fixture进行设置和清除也是try...finally
在问题注释中建议的一种更好的方法:您的单个测试专注于声明适当的条件,而不是资源清除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.