71
loading...
This website collects cookies to deliver better user experience
fake it before you make it
mock it before you rock it
patch
, mock
, side_effect
and return_value
.@patch("app.function")
def test_01_another_function(self, mock_function):
pass
MagicMock
.function
.from unittest.mock import patch, MagicMock
from app.module import RandomObject
@patch("app.module.RandomObject")
def test_01_another_function(self, mock_random):
mock_random.function.return_value = "test-value"
random_object = RandomObject()
self.assertEqual(random_object.function(), "test-value")
from unittest.mock import patch, MagicMock
from app.module import RandomObject
@patch("app.module.RandomObject")
def test_01_another_function(self, mock_random):
mock_random.function.side_effect = Exception("test-message")
random_object = RandomObject()
self.assertRaises(random_object.function(), Exception)
from unittest.mock import patch, MagicMock
mock = MagicMock()
side_effect_list = ["dummy_val", {"dummy":"value"}] # list of values on which we want to be returned.
mock.side_effect = side_effect_list
mock("test")
>>> 'dummy_val'
mock("test")
>>> {'dummy': 'value'}
from unittest.mock import patch, MagicMock
foo_dict = {"foo": "bar", "bar": "foo"}
def foo_function(key): # function which we need to bind with side effect
return foo_dict[key]
mock = MagicMock()
mock.side_effect = foo_function
mock("foo")
>>> 'bar'
mock("bar")
>>> 'foo'
get
set
hset
hget
exists
class MockRedis:
def __init__(self, cache=dict()):
self.cache = cache
get
functionality. The get method will simply take a key and return its value.def get(self, key):
if key in self.cache:
return self.cache[key]
return None # return nil
def set(self, key, value, *args, **kwargs):
if self.cache:
self.cache[key] = value
return "OK"
return None # return nil in case of some issue
class MockRedis:
def __init__(self, cache=dict()):
self.cache = cache
def get(self, key):
if key in self.cache:
return self.cache[key]
return None # return nil
def set(self, key, value, *args, **kwargs):
if self.cache:
self.cache[key] = value
return "OK"
return None # return nil in case of some issue
def hget(self, hash, key):
if hash in self.cache:
if key in self.cache[hash]:
return self.cache[hash][key]
return None # return nil
def hset(self, hash, key, value, *args, **kwargs):
if self.cache:
self.cache[hash][key] = value
return 1
return None # return nil in case of some issue
def exists(self, key):
if key in self.cache:
return 1
return 0
def cache_overwrite(self, cache=dict()):
self.cache = cache
from mock_redis_method import MockRedis
from unittest.mock import patch, MagicMock
@patch("redis.StrictRedis")
def test_01_redis(self, mock_redis):
# initialising the cache with test values
redis_cache = {
"foo": "bar",
"foobar": {"Foo": "Bar"}
}
mock_redis_obj = MockRedis(redis_cache)
# binding a side_effect of a MagicMock instance with redis methods we defined in the MockRedis class.
mock_redis_method = MagicMock()
mock_redis_method.hget = Mock(side_effect=mock_redis_obj.get)
mock_redis_method.hget = Mock(side_effect=mock_redis_obj.hget)
mock_redis_method.set = Mock(side_effect=mock_redis_obj.set)
mock_redis_method.hset = Mock(side_effect=mock_redis_obj.hset)
mock_redis_method.exists = Mock(side_effect=mock_redis_obj.exists)
# StrictRedis mock return_values is set as above mock_redis_method.
mock_redis.return_value = mock_redis_method
@patch("requests.get")
@patch("requests.post")
def test_02_external_api_calls(self, *mocks):
# request and response are mapped in a dict
request_response_dict = {
"https://dummy-host?key=foo": (
{"key": "foo"}, 200
),
"https://dummy-host?key=bar": (
{"message": "Not Found"}, 404
)
}
class MockResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code
# request.json()
def json(self):
return self.json_data
def mock_request_method(url, *args, **kwargs):
response = request_response_dict[url]
return MockResponse(response[0], response[1])
# get and post method return similar kind of response.
mocks[0].side_effect = mock_request_method
mocks[1].side_effect = mock_request_method