5.1. Test Patch

5.1.1. SetUp

>>> from unittest import TestCase
>>> from unittest.mock import patch

5.1.2. Patch Randint

Code:

>>> from random import randint
>>>
>>> def throw_dice():
...     return randint(1,6)

Test:

>>>
>>> with patch('random.randint', return_value=4) as randint:
...     assert throw_dice() == 4

5.1.3. Path Date

>>> def test_birthdate_age(self):
...     mark = User('Mark', 'Watney', birthdate='2000-01-02')
...     with patch('main.date') as d:
...         d.today.return_value = date(2024, 1, 2)
...         age = mark.get_age()
...     self.assertEqual(age, 24)