Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the pytest module.
Flask
import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing flask instead of pytest
Using unittest which is a different testing framework
✗ Incorrect
We import pytest to use its testing features including fixtures.
2fill in blank
mediumComplete the code to define a pytest fixture that creates a Flask app instance.
Flask
@pytest.fixture def app(): app = Flask(__name__) app.config['TESTING'] = [1] return app
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting TESTING to False disables testing features
Using None or 0 instead of True
✗ Incorrect
Setting TESTING to True enables testing mode in Flask.
3fill in blank
hardFix the error in the test function to use the fixture correctly.
Flask
def test_home([1]): response = app.test_client().get('/') assert response.status_code == 200
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a parameter name that does not match the fixture
Not passing any parameter to the test function
✗ Incorrect
The fixture named app must be passed as a parameter to the test function.
4fill in blank
hardFill both blanks to create a fixture that provides a test client from the Flask app fixture.
Flask
@pytest.fixture def client([1]): return [2].test_client()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong fixture name as parameter
Returning something other than the test client
✗ Incorrect
The client fixture depends on the app fixture and returns its test client.
5fill in blank
hardFill all three blanks to write a test using the client fixture to check the home page response.
Flask
def test_home_page([1]): response = [2].get('/') assert response.[3] == 200
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using app instead of client in the test function
Checking a wrong attribute instead of status_code
✗ Incorrect
The test function uses the client fixture to send a GET request and checks the status_code.