Test fixtures help set up the environment for tests so they run smoothly and reliably. They prepare things like data or app states before tests start.
Test fixtures with pytest in Flask
import pytest @pytest.fixture def fixture_name(): # setup code yield resource # teardown code
Use @pytest.fixture to mark a function as a fixture.
The yield keyword splits setup (before yield) and teardown (after yield).
import pytest @pytest.fixture def app(): from myapp import create_app app = create_app() yield app
app fixture to create a test client for HTTP requests.import pytest @pytest.fixture def client(app): return app.test_client()
import pytest @pytest.fixture def sample_data(): data = {'name': 'Alice', 'age': 30} yield data # no teardown needed here
This example shows two fixtures: app creates a Flask app, and client creates a test client. The test test_hello uses the client to call the '/hello' route and checks the response.
import pytest from flask import Flask, jsonify @pytest.fixture def app(): app = Flask(__name__) @app.route('/hello') def hello(): return jsonify(message='Hello, world!') yield app @pytest.fixture def client(app): return app.test_client() def test_hello(client): response = client.get('/hello') assert response.status_code == 200 assert response.json == {'message': 'Hello, world!'}
Fixtures can depend on other fixtures by listing them as parameters.
Use fixtures to keep tests clean and avoid repeating setup code.
Teardown code after yield runs after the test finishes, useful for cleanup.
Fixtures prepare and clean up test environments automatically.
Use @pytest.fixture to create reusable setup code.
Fixtures can provide Flask app instances, test clients, or test data.
Practice
Solution
Step 1: Understand what fixtures do in pytest
Fixtures prepare the environment for tests by setting up resources before tests run and cleaning them after.Step 2: Identify the role of fixtures in Flask testing
Fixtures help create test clients, databases, or other resources needed for Flask tests automatically.Final Answer:
To set up and tear down resources needed for tests automatically -> Option CQuick Check:
Fixtures = setup and cleanup [OK]
- Confusing fixtures with application code
- Thinking fixtures handle user input
- Assuming fixtures deploy the app
Solution
Step 1: Check the use of @pytest.fixture decorator
The @pytest.fixture decorator is required to mark a fixture. One option lacks it.Step 2: Identify correct use of yield for setup and teardown
@pytest.fixture def client(): app = Flask(__name__) yield app.test_client() uses yield to provide the test client and allows cleanup after tests, which is best practice.Final Answer:
@pytest.fixture\ndef client():\n app = Flask(__name__)\n yield app.test_client() -> Option AQuick Check:
Use @pytest.fixture and yield for fixtures [OK]
- Omitting @pytest.fixture decorator
- Returning instead of yielding
- Not yielding or returning the test client
@pytest.fixture
def sample():
print('Setup')
yield 'data'
print('Teardown')
def test_example(sample):
print(f'Test received: {sample}')Solution
Step 1: Understand fixture execution order
Before the test runs, the fixture prints 'Setup', then yields 'data' to the test.Step 2: Analyze test function output and fixture teardown
The test prints 'Test received: data'. After test finishes, fixture resumes and prints 'Teardown'.Final Answer:
Setup\nTest received: data\nTeardown -> Option AQuick Check:
Fixture setup -> test -> fixture teardown [OK]
- Assuming teardown runs before test
- Ignoring yield pauses fixture
- Thinking test runs before setup
@pytest.fixture
def client():
app = Flask(__name__)
client = app.test_client()
return client
print('Cleanup done')Solution
Step 1: Check code after return statement
Code after 'return' is unreachable, so 'print' will never run.Step 2: Confirm other parts are correct
The decorator is present, 'app.test_client()' is valid, and 'return' is allowed but limits cleanup.Final Answer:
The 'print' statement after 'return' will never execute -> Option DQuick Check:
Code after return is unreachable [OK]
- Thinking print runs after return
- Assuming return disallows fixture
- Confusing method names
Solution
Step 1: Choose fixture scope for module-wide setup
Scope 'module' runs once per module, ideal for shared DB setup across tests.Step 2: Use yield to allow teardown after all tests
Yield pauses fixture to run tests, then runs teardown after all tests complete.Final Answer:
@pytest.fixture(scope='module')\ndef temp_db():\n db = setup_temp_db()\n yield db\n teardown_temp_db(db) -> Option BQuick Check:
Module scope + yield for setup and cleanup [OK]
- Using function scope causes repeated setup
- Returning without yield skips cleanup
- Placing cleanup code after return
Ready to prove it?
Pass the test and this pattern moves unknown
