Mocked API Test
Write a test using unittest.mock for a function that depends on an external API call, simulating a successful response without making a real network call.
Approach: getweatherdescription(weatherapicall) calls whatever function it's given and reads a "condition" field from the result. Instead of a real API, pass in a MagicMock configured to return a fixed dictionary — exactly what unittest.mock.patch would set up, just constructed directly here.
Input: One line: the weather condition to simulate (e.g. sunny).
Output: One line: Test passed: The weather is <condition>.
sunny
Test passed: The weather is sunny
- 1 <= length of condition <= 50
Hint 1
MagicMock(return_value={"condition": condition}) creates a fake, callable stand-in that returns exactly that dictionary whenever it's called.
Hint 2
Pass the mock directly into get_weather_description(mock_api) — the function has no idea it isn't a real API call.
Hint 3
assert result == f"The weather is {condition}" confirms the function processed the mocked data correctly.
MagicMock(returnvalue={"condition": condition}) builds a fake stand-in for the real weather API — calling it returns the fixed dictionary you configured, with no network involved. Passing that mock into getweather_description() exercises the function's real logic (reading data['condition'] and formatting the message) completely independently of whether any real API exists — exactly what mocking is for.