-
Notifications
You must be signed in to change notification settings - Fork 116
Description
Difficulty: 2 (Medium)
Description:
This project introduces students to Python's unittest.mock
module, a standard and powerful tool for creating mock objects and testing code that interacts with external systems. In this project, we will build a simple Bitcoin price alert system and test its behavior using mocked API responses.
Technology:
unittest.mock
is part of Python's standard library. It allows developers to simulate external dependencies such as APIs or databases. This is useful for isolating test environments, simulating edge cases, and ensuring correctness without relying on real-time or real-world data.
Goal:
Create a Bitcoin price alert system that:
- Fetches Bitcoin price from CoinGecko (real or mocked)
- Compares price to a user-defined threshold
- Raises alert if threshold is crossed
- Tests all logic using
unittest.mock
Steps:
- Develop
get_price
,check_alert
, andsend_alert
functions - Implement real API call logic (using requests)
- Use
unittest.mock.patch()
to simulate different price scenarios - Write unit tests for each component
- Package into
Mock_utils.py
, and show working examples via notebook + markdown
**Useful resources:
unittest.mock Documentation
Real Python: Understanding the Python Mock Object Library
CoinGecko API Documentation
**Is it free? Yes, unittest.mock is part of Python's standard library and is free to use. CoinGecko provides free access to its API with certain rate limits, suitable for educational purposes.
**Python libraries / bindings:
unittest.mock: For creating mock objects and patching dependencies during testing.
requests: To make HTTP requests for fetching data from APIs.
pandas: For data manipulation and analysis.
This project offers students practical experience in developing applications that interact with external APIs and implementing unit tests using mock objects to ensure code reliability without depending on live data.