0
0
Flaskframework~5 mins

Mocking external services in Flask - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is mocking in the context of testing Flask applications?
Mocking means creating fake versions of external services or functions so tests can run without calling real services. It helps test your code safely and quickly.
Click to reveal answer
beginner
Why do we mock external services in Flask tests?
We mock external services to avoid slow or unreliable network calls, control test data, and test how our app handles different responses without depending on real services.
Click to reveal answer
beginner
Which Python library is commonly used to mock external services in Flask tests?
The 'unittest.mock' library is commonly used to replace parts of your system with mock objects during tests.
Click to reveal answer
intermediate
How does the 'patch' decorator help in mocking external services?
The 'patch' decorator temporarily replaces a real function or service with a mock version during a test, so the real external call is not made.
Click to reveal answer
intermediate
What is a common pattern to mock an HTTP request to an external API in Flask tests?
You mock the function that makes the HTTP request (like 'requests.get') to return a fake response object with the data you want for your test.
Click to reveal answer
What is the main benefit of mocking external services in Flask tests?
ATo skip writing tests
BTo make the app run slower
CTo increase dependency on external APIs
DTo avoid real network calls and speed up tests
Which Python module provides tools to mock functions in Flask tests?
Aunittest.mock
Bflask.mock
Crequests
Djson
What does the 'patch' decorator do in mocking?
ADeletes the function permanently
BRuns the real function twice
CReplaces a real function with a mock during a test
DCreates a new Flask app
When mocking an HTTP request, what should the mock return?
AA fake response object with test data
BAn error to stop the test
CNothing at all
DThe real HTTP response
Why is it important to control test data when mocking external services?
ATo make tests unpredictable
BTo test how your app handles different responses
CTo avoid writing test cases
DTo slow down the test suite
Explain how you would mock an external API call in a Flask test using 'unittest.mock'.
Think about replacing 'requests.get' or similar with a mock.
You got /4 concepts.
    Why is mocking external services important for reliable and fast Flask tests?
    Consider what happens if tests depend on real external services.
    You got /4 concepts.