Challenge - 5 Problems
Yield Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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"
Attempts:
2 left
💡 Hint
Remember that code before yield runs before the test, and code after yield runs after the test.
✗ Incorrect
The fixture runs setup code before yield, then the test runs using the yielded value, then teardown code runs after the test finishes.
❓ assertion
intermediate2: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?
Attempts:
2 left
💡 Hint
The assertion runs inside the test, before teardown code runs.
✗ Incorrect
The teardown code runs after the test finishes, so inside the test the list only has 'test'. The 'done' item is appended after the test ends.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
A pytest fixture must yield exactly once; multiple yields cause runtime issues.
✗ Incorrect
Pytest expects the fixture generator to yield once and then finish. Multiple yields cause a RuntimeError because the generator does not stop as expected.
🧠 Conceptual
advanced2:00remaining
Purpose of yield in pytest fixture teardown
Why does pytest use yield in fixtures for teardown instead of separate setup and teardown functions?
Attempts:
2 left
💡 Hint
Think about how yield separates setup and teardown in one function.
✗ Incorrect
Using yield in fixtures lets you write setup code before yield and teardown code after yield in the same function, making tests cleaner and easier to maintain.
❓ framework
expert3: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
Attempts:
2 left
💡 Hint
Fixtures are set up in order of parameters, but teardown happens in reverse order.
✗ Incorrect
Pytest sets up fixtures in the order they appear in the test function parameters, then runs the test, then tears down fixtures in reverse order.