Last active
January 5, 2018 17:28
-
-
Save zkan/c2370f812ecf1d5186f0e526a39f9bf2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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