Skip to content

Commit 53c2052

Browse files
authored
unittest.mock Final QA (#550)
* Final QA * Final QA on README * Sort imports according to Ruff
1 parent 101cacf commit 53c2052

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

unittest-mock/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This folder contains code examples related to the RealPython tutorial on [Understanding the Python Mock Object Library](https://realpython.com/python-mock-library/).
44

5-
The example code showcases some of the different use cases of `unittest.mock` in a single test file, `test_my_calendar.py`. When writing your own tests, keep in mind that readability counts and that your test code will be more readable if you keep one consistent approach to mocking.
5+
The example code showcases some of the different use cases of `unittest.mock` in a single test file, `test_holidays.py`. When writing your own tests, keep in mind that readability counts and that your test code will be more readable if you keep one consistent approach to mocking.
66

77
## Installation
88

@@ -23,7 +23,7 @@ $ source venv/bin/activate
2323
## Run the Tests
2424

2525
```sh
26-
(venv) $ python test_my_calendar.py
26+
(venv) $ python test_holidays.py
2727
```
2828

2929
All the tests should pass. Go ahead and play with the code examples, change them, break them, and practice your understanding of using `unittest.mock` for testing in Python.
@@ -34,4 +34,4 @@ Martin Breuss - Email: [email protected]
3434

3535
## License
3636

37-
Distributed under the MIT license. See ``LICENSE`` for more information.
37+
Distributed under the MIT license. See `LICENSE` for more information.
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
1-
import datetime
21
import unittest
2+
from datetime import datetime
33
from unittest.mock import Mock, patch
44

55
import requests
6-
from my_calendar import get_holidays, is_weekday
6+
from holidays import get_holidays, is_weekday
77
from requests.exceptions import Timeout
88

99

1010
class TestCalendar(unittest.TestCase):
1111
@classmethod
1212
def setUpClass(cls):
13-
cls.wednesday = datetime.datetime(year=2025, month=1, day=1)
14-
cls.sunday = datetime.datetime(year=2025, month=1, day=5)
13+
cls.wednesday = datetime(year=2025, month=1, day=1)
14+
cls.sunday = datetime(year=2025, month=1, day=5)
1515
cls.holidays = {"12/25": "Christmas", "7/4": "Independence Day"}
1616
cls.response_setup_dict = {
17-
"json.return_value": TestCalendar.holidays,
17+
"json.return_value": cls.holidays,
1818
"status_code": 200,
1919
}
2020

2121
def log_request(self, url):
2222
"""Helper function that logs and returns a mock successful request."""
2323
print(f"Making a request to {url}.")
2424
print("Request received!")
25-
response_mock = Mock(**TestCalendar.response_setup_dict)
26-
return response_mock
25+
return Mock(**self.response_setup_dict)
2726

28-
@patch("my_calendar.datetime")
27+
@patch("holidays.datetime")
2928
def test_is_weekday_returns_true_on_weekdays(self, mock_datetime):
30-
mock_datetime.today.return_value = TestCalendar.wednesday
29+
mock_datetime.today.return_value = self.wednesday
3130
self.assertTrue(is_weekday())
3231

33-
@patch("my_calendar.datetime")
32+
@patch("holidays.datetime")
3433
def test_is_weekday_returns_false_on_weekends(self, mock_datetime):
35-
mock_datetime.today.return_value = TestCalendar.sunday
34+
mock_datetime.today.return_value = self.sunday
3635
self.assertFalse(is_weekday())
3736

3837
# Example of patching only a specific object
@@ -43,13 +42,13 @@ def test_get_holidays_timeout(self, mock_requests):
4342

4443
# Example of using the `with` statement with `patch()`
4544
def test_get_holidays_logging(self):
46-
with patch("my_calendar.requests") as mock_requests:
45+
with patch("holidays.requests") as mock_requests:
4746
mock_requests.get.side_effect = self.log_request
4847
self.assertEqual(get_holidays()["12/25"], "Christmas")
4948

50-
@patch("my_calendar.requests")
49+
@patch("holidays.requests")
5150
def test_get_holidays_retry(self, mock_requests):
52-
response_mock = Mock(**TestCalendar.response_setup_dict)
51+
response_mock = Mock(**self.response_setup_dict)
5352
# Set the side effect of .get()
5453
mock_requests.get.side_effect = [Timeout, response_mock]
5554
# Test that the first request raises a Timeout

0 commit comments

Comments
 (0)