Skip to content

Instantly share code, notes, and snippets.

@zkan
Created January 5, 2018 17:09
Show Gist options
  • Save zkan/86ec3e2306f5c286e2e282d262308c73 to your computer and use it in GitHub Desktop.
Save zkan/86ec3e2306f5c286e2e282d262308c73 to your computer and use it in GitHub Desktop.
import random
import unittest
from unittest.mock import patch
class TryMockUsingContextManagerTest(unittest.TestCase):
def test_mock_random_twice(self):
with patch('__main__.random.randrange') as mock_randrange:
with patch('__main__.random.randint') as mock_randint:
try_mock_using_context_manager()
mock_randint.assert_called_once_with(1, 2)
mock_randrange.assert_called_once_with(3, 4)
def try_mock_using_context_manager():
random.randint(1, 2)
random.randrange(3, 4)
unittest.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment