Bird
Raised Fist0
PyTesttesting~20 mins

Fixture teardown (yield) in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Yield Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pytest fixture with yield
What will be the output when running this pytest test with the given fixture?
PyTest
import pytest

@pytest.fixture
def resource():
    print("Setup resource")
    yield "data"
    print("Teardown resource")

def test_example(resource):
    print(f"Using {resource}")
    assert resource == "data"
AUsing data\nSetup resource\nTeardown resource
BSetup resource\nTeardown resource\nUsing data
CSetup resource\nUsing data\nTeardown resource
DUsing data\nTeardown resource\nSetup resource
Attempts:
2 left
💡 Hint
Remember that code before yield runs before the test, and code after yield runs after the test.
❓ assertion
intermediate
2:00remaining
Correct assertion for fixture teardown effect
Given a pytest fixture that yields a list and appends an item after yield, which assertion correctly verifies the list contents after the test?
PyTest
import pytest

@pytest.fixture
def my_list():
    data = []
    yield data
    data.append('done')

def test_modify_list(my_list):
    my_list.append('test')
    # Which assertion below correctly checks the list after test?
Aassert my_list == ['done']
Bassert my_list == ['test', 'done']
Cassert my_list == []
Dassert my_list == ['test']
Attempts:
2 left
💡 Hint
The assertion runs inside the test, before teardown code runs.
🔧 Debug
advanced
2:00remaining
Identify the error in fixture teardown with yield
What error will this pytest fixture cause when used in a test?
PyTest
import pytest

@pytest.fixture
def broken_fixture():
    print("Setup")
    yield
    print("Teardown")
    yield

def test_broken(broken_fixture):
    assert True
ASyntaxError: multiple yield statements in fixture
BRuntimeError: generator didn't stop after second yield
CTypeError: fixture must yield exactly once
DNo error, test passes normally
Attempts:
2 left
💡 Hint
A pytest fixture must yield exactly once; multiple yields cause runtime issues.
🧠 Conceptual
advanced
2:00remaining
Purpose of yield in pytest fixture teardown
Why does pytest use yield in fixtures for teardown instead of separate setup and teardown functions?
AYield allows setup and teardown code to be in one place, improving readability and resource management.
BYield makes the fixture run twice, once for setup and once for teardown.
CYield is used to pause the test execution until teardown is manually triggered.
DYield automatically retries the test if it fails during setup.
Attempts:
2 left
💡 Hint
Think about how yield separates setup and teardown in one function.
❓ framework
expert
3:00remaining
Order of execution in multiple yield fixtures
Given two pytest fixtures using yield, what is the correct order of printed output when both are used in a test?
PyTest
import pytest

@pytest.fixture
def fixture_a():
    print("Setup A")
    yield
    print("Teardown A")

@pytest.fixture
def fixture_b():
    print("Setup B")
    yield
    print("Teardown B")

def test_both(fixture_a, fixture_b):
    print("Test running")
    assert True
ASetup A\nSetup B\nTest running\nTeardown B\nTeardown A
BSetup B\nSetup A\nTest running\nTeardown A\nTeardown B
CSetup B\nSetup A\nTest running\nTeardown B\nTeardown A
DSetup A\nSetup B\nTest running\nTeardown A\nTeardown B
Attempts:
2 left
💡 Hint
Fixtures are set up in order of parameters, but teardown happens in reverse order.

Practice

(1/5)
1. What is the main purpose of using yield in a pytest fixture?
easy
A. To separate setup and teardown code in the fixture
B. To skip the test execution
C. To run the test multiple times
D. To mark the test as expected to fail

Solution

  1. Step 1: Understand fixture setup and teardown

    In pytest, setup code prepares resources before a test, and teardown code cleans up after the test.
  2. Step 2: Role of yield in fixture

    The yield statement splits the fixture into two parts: code before yield runs before the test (setup), and code after yield runs after the test (teardown).
  3. Final Answer:

    To separate setup and teardown code in the fixture -> Option A
  4. Quick Check:

    Yield separates setup and teardown [OK]
Hint: Remember: yield splits setup and cleanup in fixtures [OK]
Common Mistakes:
  • Thinking yield skips tests
  • Confusing yield with test repetition
  • Using yield to mark test outcomes
2. Which of the following is the correct syntax for a pytest fixture using yield for teardown?
easy
A. def my_fixture(): setup() yield teardown()
B. def my_fixture(): yield setup() teardown()
C. def my_fixture(): setup() teardown() yield
D. def my_fixture(): teardown() yield setup()

Solution

  1. Step 1: Identify setup and teardown order

    Setup code must run before yield, teardown code after yield.
  2. Step 2: Check each option's order

    def my_fixture(): setup() yield teardown() correctly places setup before yield and teardown after. Others have incorrect order.
  3. Final Answer:

    def my_fixture(): setup() yield teardown() -> Option A
  4. Quick Check:

    Setup before yield, teardown after [OK]
Hint: Setup before yield, teardown after yield in fixture [OK]
Common Mistakes:
  • Placing teardown before yield
  • Calling setup after yield
  • Yield placed at the end after teardown
3. What will be the output when running this pytest fixture and test?
import pytest

@pytest.fixture
def resource():
    print('Setup')
    yield
    print('Teardown')

def test_example(resource):
    print('Test running')
medium
A. Test running\nSetup\nTeardown
B. Setup\nTeardown\nTest running
C. Setup\nTest running\nTeardown
D. Teardown\nSetup\nTest running

Solution

  1. Step 1: Trace fixture execution order

    Fixture prints 'Setup' before yield, then test runs printing 'Test running', then fixture prints 'Teardown' after yield.
  2. Step 2: Combine output sequence

    Output order is 'Setup', 'Test running', then 'Teardown'.
  3. Final Answer:

    Setup\nTest running\nTeardown -> Option C
  4. Quick Check:

    Setup -> Test -> Teardown order [OK]
Hint: Yield splits setup (before) and teardown (after) print order [OK]
Common Mistakes:
  • Assuming teardown runs before test
  • Mixing print order
  • Ignoring yield effect on execution flow
4. Identify the error in this pytest fixture using yield:
@pytest.fixture
def faulty_fixture():
    yield
    setup()
    teardown()
medium
A. Teardown code must be before yield
B. Yield cannot be used in fixtures
C. Fixture must return a value instead of yield
D. Setup code is after yield, so it runs after the test instead of before

Solution

  1. Step 1: Analyze code order around yield

    Setup code should run before yield to prepare resources; here it is after yield, so it runs after the test.
  2. Step 2: Understand consequences

    This means setup runs too late, breaking test assumptions. Teardown after setup is also misplaced.
  3. Final Answer:

    Setup code is after yield, so it runs after the test instead of before -> Option D
  4. Quick Check:

    Setup must be before yield [OK]
Hint: Setup must be before yield, teardown after [OK]
Common Mistakes:
  • Placing setup after yield
  • Thinking yield is not allowed
  • Confusing teardown position
5. You want to create a pytest fixture that opens a file before a test and closes it after. Which fixture code correctly uses yield for this purpose?
hard
A. def file_fixture(): yield open('data.txt') f.close()
B. def file_fixture(): f = open('data.txt') yield f f.close()
C. def file_fixture(): f = open('data.txt') f.close() yield f
D. def file_fixture(): f = open('data.txt') yield f.close()

Solution

  1. Step 1: Open file before yield and yield file object

    def file_fixture(): f = open('data.txt') yield f f.close() opens the file, yields the file object to the test, allowing test to use it.
  2. Step 2: Close file after yield

    After test finishes, code after yield closes the file, ensuring cleanup.
  3. Final Answer:

    def file_fixture(): f = open('data.txt') yield f f.close() -> Option B
  4. Quick Check:

    Open before yield, yield resource, close after yield [OK]
Hint: Yield resource to test, cleanup after yield [OK]
Common Mistakes:
  • Closing file before yield
  • Yielding without resource variable
  • Not closing file after test
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard