Bird
0
0

Given this Flask test code snippet, what will be printed?

medium📝 component behavior Q13 of 15
Flask - Testing Flask Applications
Given this Flask test code snippet, what will be printed?
from unittest.mock import patch
import requests

def fetch_data():
    response = requests.get('http://example.com')
    return response.status_code

with patch('requests.get') as mock_get:
    mock_get.return_value.status_code = 404
    print(fetch_data())
ANone
B200
C404
DRaises an error
Step-by-Step Solution
Solution:
  1. Step 1: Understand patch effect on requests.get

    The patch replaces requests.get with a mock whose return_value.status_code is set to 404.
  2. Step 2: Trace fetch_data call inside patch

    fetch_data calls requests.get, which returns the mock with status_code 404, so fetch_data returns 404.
  3. Final Answer:

    404 -> Option C
  4. Quick Check:

    Mocked status_code = 404 [OK]
Quick Trick: Mock return_value sets fake response attributes [OK]
Common Mistakes:
MISTAKES
  • Expecting real HTTP 200 response
  • Confusing mock object with None
  • Thinking patch causes error

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes