0
0
PyTesttesting~20 mins

Fixture finalization (request.addfinalizer) in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fixture Finalizer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Understanding fixture finalization order with request.addfinalizer

What will be the output order of the print statements when running this pytest test?

PyTest
import pytest

def test_example(request):
    print("Start test")
    request.addfinalizer(lambda: print("First finalizer"))
    request.addfinalizer(lambda: print("Second finalizer"))
    print("End test")
AStart test\nEnd test\nFirst finalizer\nSecond finalizer
BFirst finalizer\nSecond finalizer\nStart test\nEnd test
CStart test\nEnd test\nSecond finalizer\nFirst finalizer
DStart test\nFirst finalizer\nEnd test\nSecond finalizer
Attempts:
2 left
💡 Hint

Remember that finalizers added later run first.

assertion
intermediate
2:00remaining
Correct assertion to verify finalizer execution

You want to test that a finalizer function was called after the test. Which assertion correctly verifies this?

PyTest
import pytest

called = []

def test_finalizer_called(request):
    def cleanup():
        called.append(True)
    request.addfinalizer(cleanup)
    assert ???
Acalled == []
Bcalled == [True]
Ccalled is None
Dcalled == [False]
Attempts:
2 left
💡 Hint

Finalizers run after the test function finishes.

🔧 Debug
advanced
2:00remaining
Identify the error in fixture finalizer usage

What error will this pytest fixture cause?

PyTest
import pytest

@pytest.fixture
def resource(request):
    print("Setup resource")
    request.addfinalizer("cleanup")
    return "resource"
ANo error, runs fine
BTypeError: 'str' object is not callable
CNameError: name 'cleanup' is not defined
DSyntaxError: invalid syntax
Attempts:
2 left
💡 Hint

Check what type addfinalizer expects as argument.

🧠 Conceptual
advanced
2:00remaining
Why use request.addfinalizer instead of yield in fixtures?

Which reason best explains when to prefer request.addfinalizer over yield in pytest fixtures?

AWhen you need multiple finalizers to run in a specific order
BWhen the fixture does not require any cleanup
CWhen you want to skip the test conditionally
DWhen you want to run cleanup code before the test starts
Attempts:
2 left
💡 Hint

Think about multiple cleanup steps and their order.

framework
expert
3:00remaining
Predict the final output of nested finalizers in pytest

Given this pytest test, what is the exact printed output?

PyTest
import pytest

def test_nested_finalizers(request):
    print("Test start")
    def first_cleanup():
        print("First cleanup start")
        def second_cleanup():
            print("Second cleanup")
        request.addfinalizer(second_cleanup)
        print("First cleanup end")
    request.addfinalizer(first_cleanup)
    print("Test end")
ATest start\nTest end\nFirst cleanup end\nFirst cleanup start\nSecond cleanup
BTest start\nTest end\nFirst cleanup start\nSecond cleanup\nFirst cleanup end
CTest start\nTest end\nSecond cleanup\nFirst cleanup start\nFirst cleanup end
DTest start\nTest end\nFirst cleanup start\nFirst cleanup end\nSecond cleanup
Attempts:
2 left
💡 Hint

Remember that finalizers run after the test, and added finalizers run in reverse order.