Given the following pytest fixture and test, what will be the output when running the test?
import pytest from flask import Flask @pytest.fixture def client(): app = Flask(__name__) @app.route('/') def home(): return 'Hello, Flask!' with app.test_client() as client: yield client def test_home(client): response = client.get('/') return response.data.decode()
Think about what the route returns and how the test client fetches it.
The fixture creates a Flask app with a route '/' that returns 'Hello, Flask!'. The test client calls this route and gets the response data, which is the string 'Hello, Flask!'.
Consider this pytest fixture with session scope and two tests using it. What is the final value of counter['count'] after both tests run?
import pytest counter = {'count': 0} @pytest.fixture(scope='session') def count_fixture(): counter['count'] += 1 yield counter['count'] += 1 def test_one(count_fixture): pass def test_two(count_fixture): pass
Remember session scope means the fixture runs once per test session.
The fixture runs once before any tests using it, incrementing count to 1. Then both tests run. After all tests finish, the fixture's teardown runs, incrementing count to 2. But since the fixture yields once, the increment before yield and after yield happen once each, so total increments are 2. However, the counter is incremented before yield and after yield only once per session, so final count is 2.
Correction: Actually, the fixture increments before yield (count=1), then both tests run, then after all tests finish, increments after yield (count=2). So final count is 2.
Identify which fixture code snippet will cause a syntax error when run.
Look carefully at the condition inside the list comprehension.
Option D uses a single equals sign '=' inside the list comprehension condition, which is invalid syntax in Python. It should use '==' for comparison.
Given this fixture and test, why does the test fail with AttributeError: 'NoneType' object has no attribute 'get'?
import pytest from flask import Flask @pytest.fixture def client(): app = Flask(__name__) @app.route('/') def home(): return 'Hi' client = app.test_client() return client def test_home(client): response = client.get('/') assert response.data == b'Hi'
Check what the fixture returns or yields.
The fixture creates the test client but does not return or yield it. So the test function receives None instead of a client object, causing AttributeError when calling get().
Consider a Flask app tested with pytest fixtures of different scopes: function, module, and session. Which statement best describes how fixture scope affects resource setup and teardown?
Think about how often you want to create and destroy resources during testing.
Function scope means the fixture runs setup and teardown for each test function. Module scope runs once per module (file). Session scope runs once for the entire test session. This controls how often resources like Flask apps or databases are created and cleaned up.