The mock object library can be used to create a Mock instance for Redis that returns a value for a particular method call, for example:
from unittest.mock import Mock
def test_redis():
mock_redis = Mock()
mock_redis.get.return_value = "2020-12-31T00:00:00".encode()
function_to_test(mock_redis)
If a function needs to be mocked, so that the function is not actually called, and is used inside the function that is being tested, the patch decorator can be used, as follows:
from unittest.mock import patch
@patch("package.module.function_to_mock")
def test_redis(mock_function_to_mock):
mock_function_to_mock.get.return_value = "2020-12-31T00:00:00".encode()
function_to_test()
This could be used to mock the flask caching related code that is inside the function that you are testing.
Edit
For your case, I believe you may want to do the following:
from unittest.mock import patch
@patch("package.module.func2")
def test_func_1(mock_func2):
mock_func2.return_value = "Test value"
func_1()
mock_func2.assert_called_once()
Here func2()
is mocked and it's return value is set to "Test value"
, so when it is called inside of func_1()
variable x
equals "Test value"
Note: the path passed to patch()
must be to where func2()
is imported, and not to the module where func2()
is defined.