Pytest Finalizers - order of execution

别等时光非礼了梦想. 提交于 2019-12-10 18:56:18

问题


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.

Thanks in advance.


回答1:


I guess the easiest way would be to just try running your code with -s and see in which order the prints happen.

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

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.



来源:https://stackoverflow.com/questions/38665397/pytest-finalizers-order-of-execution

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!