Context manager fixtures help set up and clean up resources automatically during tests. They make tests easier to write and keep clean.
Context manager fixtures in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
PyTest
import pytest @pytest.fixture def resource(): with setup_resource() as res: yield res
The yield keyword pauses the fixture to run the test, then resumes to clean up.
Use with to manage the resource safely inside the fixture.
Examples
PyTest
import pytest @pytest.fixture def open_file(): with open('test.txt', 'w') as f: yield f
PyTest
import pytest class Server: def start(self): print('Server started') def stop(self): print('Server stopped') @pytest.fixture def server(): srv = Server() srv.start() yield srv srv.stop()
Sample Program
This test uses a context manager fixture to set up and clean up a resource. The print statements show the order of actions.
PyTest
import pytest class Resource: def __enter__(self): print('Resource setup') return self def __exit__(self, exc_type, exc_val, exc_tb): print('Resource cleanup') @pytest.fixture def resource(): with Resource() as res: yield res def test_example(resource): print('Test is running') assert True
Important Notes
Always use yield in context manager fixtures to separate setup and cleanup.
Context manager fixtures help avoid leftover resources that can cause flaky tests.
Summary
Context manager fixtures use with and yield to manage setup and cleanup.
They make tests cleaner and safer by handling resources automatically.
Use them whenever your test needs temporary resources like files, servers, or connections.
Practice
1. What is the main purpose of using a context manager fixture in pytest?
easy
Solution
Step 1: Understand context manager role
Context managers in pytest useyieldto 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 BQuick 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
Solution
Step 1: Recall context manager fixture syntax
In pytest, the code beforeyieldruns as setup, and the code afteryieldruns as cleanup.Step 2: Check each option
@pytest.fixture def resource(): setup() yield cleanup() correctly placessetup()beforeyieldandcleanup()after. Others have wrong order.Final Answer:
@pytest.fixture def resource(): setup() yield cleanup() -> Option AQuick 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?
@pytest.fixture
def file_resource():
print('Setup file')
yield
print('Cleanup file')
def test_example(file_resource):
print('Running test')medium
Solution
Step 1: Understand fixture execution order
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 DQuick 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?
@pytest.fixture
def db_connection():
conn = connect_db()
yield conn
conn.close()medium
Solution
Step 1: Analyze fixture structure
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, soconn.close()will run properly after the test.Final Answer:
Nothing is wrong; it is correct -> Option AQuick 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
Solution
Step 1: Setup temporary file before yield
The file must be created before yielding its path so the test can use it.Step 2: Cleanup file after yield
After the test, the file should be deleted to clean up resources.Final Answer:
@pytest.fixture def temp_file(): path = '/tmp/testfile.txt' open(path, 'w').close() yield path os.remove(path) -> Option CQuick Check:
Create file before yield, delete after yield [OK]
Hint: Create resource before yield, clean up after yield [OK]
Common Mistakes:
- Deleting file before test runs
- Creating file after yield
- Not yielding the file path
