Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is a context manager fixture in pytest?
A context manager fixture in pytest is a fixture that uses the yield statement to set up a resource before a test and clean it up after the test finishes, similar to how a context manager works with with statements.
Click to reveal answer
beginner
How do you define a context manager fixture in pytest?
You define a context manager fixture by using the @pytest.fixture decorator and including a yield statement inside the fixture function. Code before yield runs before the test, and code after yield runs after the test for cleanup.
Click to reveal answer
intermediate
Why use context manager fixtures instead of regular fixtures?
Context manager fixtures help manage setup and cleanup in one place clearly and safely. They ensure resources like files, connections, or temporary data are properly released after tests, preventing side effects.
Click to reveal answer
beginner
Example: What does this pytest fixture do?
@pytest.fixture
def open_file():
f = open('test.txt', 'w')
yield f
f.close()
This fixture opens a file named 'test.txt' for writing before the test runs. It yields the file object to the test. After the test finishes, it closes the file to free the resource.
Click to reveal answer
intermediate
How does pytest know when to run the cleanup code in a context manager fixture?
Pytest runs the cleanup code immediately after the test that uses the fixture finishes. It executes the code after the yield statement in the fixture function, ensuring proper teardown.
Click to reveal answer
What keyword is used in pytest fixtures to separate setup and teardown code?
Areturn
Bbreak
Cyield
Dcontinue
✗ Incorrect
The yield keyword is used in pytest fixtures to separate setup (before yield) and teardown (after yield) code.
When is the code after yield in a pytest fixture executed?
AAfter the test finishes
BBefore the test runs
CDuring the test
DNever
✗ Incorrect
Code after yield runs after the test finishes, to clean up resources.
Which decorator is used to create a fixture in pytest?
A@pytest.test
B@pytest.fixture
C@pytest.context
D@pytest.setup
✗ Incorrect
The @pytest.fixture decorator marks a function as a fixture.
Why is it better to use context manager fixtures for resource cleanup?
AThey run faster
BThey skip tests
CThey require less code
DThey automatically handle setup and teardown in one place
✗ Incorrect
Context manager fixtures handle setup and cleanup clearly and safely in one function.
What happens if you forget to close a resource in a pytest fixture?
AResources may leak and cause errors in other tests
BPytest closes it automatically
CThe test will always pass
DNothing happens
✗ Incorrect
Not closing resources can cause leaks and affect other tests negatively.
Explain how a context manager fixture works in pytest and why it is useful.
Think about how 'with' statements work in Python and how pytest fixtures can mimic that.
You got /5 concepts.
Write a simple pytest fixture using a context manager to open and close a file for testing.
Remember to put cleanup code after the yield statement.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using a context manager fixture in pytest?
easy
A. To speed up test execution by skipping setup
B. To automatically handle setup and cleanup around a test
C. To replace assertions with print statements
D. To run tests in parallel automatically
Solution
Step 1: Understand context manager role
Context managers in pytest use yield to run setup code before the test and cleanup code after the test finishes.
Step 2: Identify purpose in testing
This automatic setup and cleanup makes tests safer and cleaner by managing resources like files or connections.
Final Answer:
To automatically handle setup and cleanup around a test -> Option B
Quick Check:
Context manager fixture = automatic setup and cleanup [OK]
Hint: Context managers wrap setup and cleanup automatically [OK]
Common Mistakes:
Thinking context managers speed up tests by skipping setup
Confusing context managers with parallel test execution
Using print statements instead of assertions
2. Which of the following is the correct way to define a context manager fixture in pytest?
easy
A. @pytest.fixture
def resource():
setup()
yield
cleanup()
B. @pytest.fixture
def resource():
yield
setup()
cleanup()
C. @pytest.fixture
def resource():
setup()
cleanup()
yield
D. @pytest.fixture
def resource():
cleanup()
yield
setup()
Solution
Step 1: Recall context manager fixture syntax
In pytest, the code before yield runs as setup, and the code after yield runs as cleanup.
Step 2: Check each option
@pytest.fixture
def resource():
setup()
yield
cleanup() correctly places setup() before yield and cleanup() after. Others have wrong order.
Final Answer:
@pytest.fixture
def resource():
setup()
yield
cleanup() -> Option A
Quick Check:
Setup before yield, cleanup after yield [OK]
Hint: Setup code goes before yield, cleanup after yield [OK]
Common Mistakes:
Placing cleanup before yield
Calling setup after yield
Putting yield at the end after cleanup
3. Given this fixture and test, what will be printed when running pytest?
Before the test runs, the fixture prints 'Setup file'. Then the test prints 'Running test'. After the test finishes, the fixture prints 'Cleanup file'.
Step 2: Match output sequence
The output order is: 'Setup file', 'Running test', 'Cleanup file'.
Final Answer:
Setup file\nRunning test\nCleanup file -> Option D
Quick Check:
Setup -> Test -> Cleanup order [OK]
Hint: Fixture setup prints before test, cleanup prints after [OK]
Common Mistakes:
Assuming cleanup runs before test
Thinking test runs before setup
Mixing order of prints
4. What is wrong with this context manager fixture?
The fixture creates a connection, yields it for the test, then closes it after the test finishes.
Step 2: Confirm cleanup runs after yield
Code after yield runs as cleanup, so conn.close() will run properly after the test.
Final Answer:
Nothing is wrong; it is correct -> Option A
Quick Check:
Yield passes resource, cleanup runs after yield [OK]
Hint: Cleanup code after yield always runs after test [OK]
Common Mistakes:
Thinking cleanup code never runs
Closing connection before yield
Not yielding the resource
5. You want to write a context manager fixture that creates a temporary file, yields its path, and deletes the file after the test. Which code correctly implements this?
hard
A. @pytest.fixture
def temp_file():
path = '/tmp/testfile.txt'
os.remove(path)
yield path