Skip to content

Instantly share code, notes, and snippets.

@leopard627
Created May 24, 2017 13:41
Show Gist options
  • Save leopard627/b3979c3e9b51424f0cbfc99c49b26a8a to your computer and use it in GitHub Desktop.
Save leopard627/b3979c3e9b51424f0cbfc99c49b26a8a to your computer and use it in GitHub Desktop.
mock_example_python2.7.xx
import unittest
from os import urandom
import mock
def simple_urandom(length):
return 'f' * length
class TestRandom(unittest.TestCase):
@mock.patch('__main__.urandom', side_effect=simple_urandom)
def test_urandom(self, urandom_function):
assert urandom(5) == 'fffff'
# from
# http://fgimian.github.io/blog/2014/04/10/using-the-python-mock-library-to-fake-regular-functions-during-tests/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment