Complete the code to create a pytest fixture that uses yield for setup and teardown.
import pytest @pytest.fixture def resource(): print("Setup resource") [1] print("Teardown resource")
The yield keyword in a pytest fixture pauses the function to provide the setup resource, then continues after the test for teardown.
Complete the fixture to yield a value that tests can use.
import pytest @pytest.fixture def data(): setup_data = {'key': 'value'} [1] setup_data print("Cleanup after test")
Using yield returns the setup data to the test, then runs cleanup after the test finishes.
Fix the error in the fixture to ensure teardown runs after the test.
import pytest @pytest.fixture def connection(): print("Open connection") [1] 'conn' print("Close connection")
Using yield pauses the fixture to provide the connection, then runs the close code after the test.
Fill both blanks to create a fixture that yields a resource and prints messages before and after the test.
import pytest @pytest.fixture def file_resource(): print("Opening file") [1] 'file' print([2])
The fixture yields the resource, then prints "File closed" after the test finishes.
Fill all three blanks to create a fixture that sets up, yields a value, and cleans up with a print message.
import pytest @pytest.fixture def setup_teardown(): print([1]) [2] 42 print([3])
The fixture prints setup message, yields the value 42, then prints teardown message after the test.