Pytest Finalizers - 执行顺序 [英] Pytest Finalizers - order of execution
问题描述
我正在编写 py.test 程序,考虑以下 py.test 夹具代码:
I am writing py.test program, considering the following py.test fixture code:
@pytest.fixture(scope="class")
def my_fixture(request):
def fin1():
print("fin1")
request.addfinalizer(fin1)
def fin2():
print("fin2")
request.addfinalizer(fin2)
执行顺序是什么?我没有在文档中找到任何关于终结器执行顺序的提及.
What the execution order? I didn't find any mentions on the documentation regarding the execution order of finalizers.
提前致谢.
推荐答案
我想最简单的方法是尝试使用 -s
运行您的代码,然后查看打印的顺序.
I guess the easiest way would be to just try running your code with -s
and see in which order the prints happen.
我的建议是使用 yield 固定装置,这样您就可以轻松地明确控制拆卸顺序:
What I'd recommend is to use yield fixtures instead, so you can explicitly control the teardown order easily:
@pytest.yield_fixture(scope="class")
def my_fixture():
# do setup
yield
fin1()
fin2()
从 pytest 3.0(即将发布)开始,这也可以通过将 yield
与普通的 @pytest.fixture
装饰器一起使用,并将成为推荐的拆解方式.
Starting with pytest 3.0 (which will be released soon), this will also work by just using yield
with the normal @pytest.fixture
decorator, and will be the recommended way of doing teardown.
这篇关于Pytest Finalizers - 执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!