Module 08 / Project 02 — Mocking¶
Learn Your Way¶
| Read | Build | Watch | Test | Review | Visualize | Try |
|---|---|---|---|---|---|---|
| — | This project | — | — | Flashcards | — | — |
Focus¶
unittest.mock.patchto replace external dependencies during testsMagicMockfor creating fake objects with any attributes or methodsside_effectfor simulating errors and varying responsesreturn_valuefor controlling what a mock returns
Why this project exists¶
Real applications talk to external services — APIs, databases, file systems. You cannot run tests that depend on a live weather API: the tests would be slow, flaky (what if the API is down?), and non-deterministic (weather changes). Mocking lets you replace the real API call with a fake one that returns exactly what you tell it to. Your tests run in milliseconds, work offline, and test your logic without testing someone else's server.
Run¶
Expected output¶
tests/test_weather.py::test_get_temperature_success PASSED
tests/test_weather.py::test_get_temperature_city_not_found PASSED
tests/test_weather.py::test_get_temperature_api_error PASSED
tests/test_weather.py::test_get_temperature_timeout PASSED
tests/test_weather.py::test_get_forecast_success PASSED
tests/test_weather.py::test_get_forecast_returns_correct_days PASSED
tests/test_weather.py::test_get_temperature_different_cities PASSED
All tests should pass without making any real HTTP requests.
Alter it¶
- Add a new method
get_humidity(city)toWeatherServiceand write a mocked test for it. - Change one of the mocked responses to return a different temperature and verify the test still checks the right thing.
- Add a test that verifies
requests.getwas called with the correct URL (usemock.assert_called_once_with).
Break it¶
- Remove the
@patchdecorator from one test and run it. What happens when the test tries to call the real API? - Change the patch target to the wrong module path (e.g.,
@patch("requests.get")instead of@patch("project.requests.get")). Why does this matter? - Make a mock return a response without a
.json()method and see what error your code raises.
Fix it¶
- Put the
@patchdecorator back and verify the test passes again. - Fix the patch target so it points to the right location.
- Add proper error handling in
WeatherServicefor responses that do not have the expected JSON structure.
Explain it¶
- Why do we patch
project.requests.getinstead of justrequests.get? - What is the difference between
return_valueandside_effect? - What happens if you forget to patch an external dependency in a test?
- How does
MagicMockknow what attributes and methods to have?
Mastery check¶
You can move on when you can:
- Write a
@patchdecorator from memory and explain the target path. - Use
side_effectto simulate both exceptions and varying return values. - Explain why mocking makes tests faster and more reliable.
- Verify that a mock was called with specific arguments.
Related Concepts¶
- Classes and Objects
- Errors and Debugging
- Functions Explained
- How Loops Work
- Quiz: Classes and Objects