Skip to content

Instantly share code, notes, and snippets.

@zkan
Last active January 5, 2018 17:28
Show Gist options
  • Save zkan/c2370f812ecf1d5186f0e526a39f9bf2 to your computer and use it in GitHub Desktop.
Save zkan/c2370f812ecf1d5186f0e526a39f9bf2 to your computer and use it in GitHub Desktop.
from contextlib import ExitStack
import random
import unittest
from unittest.mock import patch
class TryMockUsingExitStackTest(unittest.TestCase):
def test_mock_random_twice(self):
with ExitStack() as stack:
mock_randrange = stack.enter_context(
patch('__main__.random.randrange')
)
mock_randint = stack.enter_context(
patch('__main__.random.randint')
)
try_mock_using_exit_stack()
mock_randint.assert_called_once_with(1, 2)
mock_randrange.assert_called_once_with(3, 4)
def try_mock_using_exit_stack():
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