问题
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