0
0
Flaskframework~20 mins

Test fixtures with pytest in Flask - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pytest Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output when using a pytest fixture to create a Flask test client?

Given the following pytest fixture and test, what will be the output when running the test?

Flask
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()
A"Hello, Flask!"
B"<html><body>Hello, Flask!</body></html>"
CTypeError: 'Response' object is not callable
D"404 Not Found"
Attempts:
2 left
💡 Hint

Think about what the route returns and how the test client fetches it.

state_output
intermediate
2:00remaining
What is the value of the counter after two tests using a session-scoped fixture?

Consider this pytest fixture with session scope and two tests using it. What is the final value of counter['count'] after both tests run?

Flask
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
A1
B2
C3
D4
Attempts:
2 left
💡 Hint

Remember session scope means the fixture runs once per test session.

📝 Syntax
advanced
2:00remaining
Which pytest fixture code will cause a SyntaxError?

Identify which fixture code snippet will cause a syntax error when run.

A
import pytest

@pytest.fixture
def sample():
    return [x for x in range(5) if x % 2 != 0]
B
import pytest

@pytest.fixture
def sample():
    return [x for x in range(5) if x % 2 == 0]
C
import pytest

@pytest.fixture
def sample():
    return [x for x in range(5)]
D
import pytest

@pytest.fixture
def sample():
    return [x for x in range(5) if x % 2 = 0]
Attempts:
2 left
💡 Hint

Look carefully at the condition inside the list comprehension.

🔧 Debug
advanced
2:00remaining
Why does this pytest fixture cause a test to fail with AttributeError?

Given this fixture and test, why does the test fail with AttributeError: 'NoneType' object has no attribute 'get'?

Flask
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'
AThe test client is not created properly with app.test_client().
BThe route '/' is missing a return statement.
CThe fixture does not return or yield the client, so test receives None.
DThe test function is missing the @pytest.mark.usefixtures decorator.
Attempts:
2 left
💡 Hint

Check what the fixture returns or yields.

🧠 Conceptual
expert
2:00remaining
How does pytest fixture scope affect resource setup and teardown in Flask testing?

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?

AFunction scope fixtures run setup and teardown once per test function, module scope once per module, and session scope once per entire test session.
BAll fixture scopes run setup and teardown once per test function regardless of scope.
CModule scope fixtures run setup and teardown once per test function, function scope once per module, and session scope once per test function.
DSession scope fixtures run setup and teardown once per test function, module scope once per session, and function scope once per module.
Attempts:
2 left
💡 Hint

Think about how often you want to create and destroy resources during testing.