0
0
Flaskframework~10 mins

Test fixtures with pytest in Flask - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Test fixtures with pytest
Define fixture function
Mark with @pytest.fixture
Test function requests fixture
pytest runs fixture setup
Fixture returns resource
Test uses resource
Test finishes
Fixture teardown (optional)
Test suite continues or ends
This flow shows how pytest fixtures are defined, provided to tests, and optionally cleaned up after tests run.
Execution Sample
Flask
import pytest
from flask import Flask

@pytest.fixture
def app():
    app = Flask(__name__)
    yield app

    # teardown code here
Defines a pytest fixture named 'app' that creates a Flask app instance for tests.
Execution Table
StepActionEvaluationResult
1pytest finds @pytest.fixture 'app'Registers fixtureFixture 'app' ready
2Test function requests 'app' fixturepytest calls fixture functionFlask app instance created
3Fixture yields app instanceTest receives app instanceTest can use Flask app
4Test runs using appTest code executesTest passes or fails
5Test finishesFixture teardown runs after yieldResources cleaned up
6Next test or endRepeat or stopTest suite continues or ends
💡 All tests using fixture completed; fixture teardown done after each test.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5Final
appundefinedFlask app instance createdYielded to testTeardown executedNone or cleaned
Key Moments - 3 Insights
Why does the fixture function use 'yield' instead of 'return'?
Using 'yield' allows pytest to run teardown code after the test finishes, as shown in step 5 of the execution_table.
How does pytest know to run the fixture before the test?
pytest detects the @pytest.fixture decorator and sees the test requests the fixture by name, triggering the fixture call at step 2.
Can multiple tests share the same fixture instance?
By default, each test gets a fresh fixture instance, ensuring isolation, as the fixture runs setup and teardown around each test (steps 2 to 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what happens at step 3?
AThe fixture yields the Flask app instance to the test
BThe test finishes and fixture teardown runs
Cpytest registers the fixture
DThe test requests the fixture
💡 Hint
Check the 'Action' and 'Result' columns at step 3 in the execution_table
At which step does pytest run the fixture teardown code?
AStep 3
BStep 5
CStep 2
DStep 6
💡 Hint
Look for 'Fixture teardown runs after yield' in the execution_table
If a test does not request the fixture, what happens?
AThe fixture is still run before the test
Bpytest throws an error
CThe fixture is not run at all
DThe fixture runs after the test
💡 Hint
pytest only runs fixtures that tests explicitly request, as shown in step 2
Concept Snapshot
pytest fixtures provide reusable setup and teardown for tests.
Define with @pytest.fixture decorator.
Use 'yield' to separate setup and teardown.
Tests request fixtures by naming them as parameters.
Fixtures run before each test that requests them.
Teardown runs after test finishes.
Full Transcript
This visual execution trace shows how pytest fixtures work in Flask testing. First, a fixture function is defined and marked with @pytest.fixture. When a test function requests this fixture by name, pytest runs the fixture function, creating the Flask app instance. The fixture yields this instance to the test, allowing the test to use it. After the test finishes, pytest runs any teardown code after the yield. This process repeats for each test that requests the fixture, ensuring clean setup and cleanup. Key points include using yield for teardown, pytest detecting fixture usage, and fixture isolation per test.