Description of the class RedisCliWrapper
I have created a class called RedisCliWrapper where I have written many methods which call methods of the class redis.Redis. Substantially I have created a wrapper of the class redis.Redis. The reason of this choice is not important in this question.
The class RedisCliWrapper is written in the module redis_cli_wrapper.py. Below I show the class with only the method get_hgetall_value(), which is one of its many methods:
from redis import Redis
class RedisCliWrapper:
def __init__(self, redis_cli_instance : Redis):
self.__redis_cli_instance : Redis = redis_cli_instance
def get_hgetall_value(self, key):
return self.__redis_cli_instance.hgetall(key)
As the code shows when I create an instance of RedisCliWrapper I have to pass to the __init__() method an instance of the class redis.Redis.
Description of the test class RedisCliWrapperTestCase
To test the class RedisCliWrapper I have created a test module called test_redis_cli_wrapper.py. The content of the module is the following:
import unittest
from unittest import mock
import redis
from redis_cli_wrapper import RedisCliWrapper
class RedisCliWrapperTestCase(unittest.TestCase):
def setUp(self):
self.mock_redis_cli = mock.create_autospec(redis.Redis)
#self.mock_redis_cli = mock.Mock()
self.sut = RedisCliWrapper(self.mock_redis_cli)
def test_something_1(self):
self.mock_redis_cli.hgetall.return_value = {‘f1’: ‘value1’, ‘f2’: ‘value2’}
self.sut.get_hgetall_value(‘key1’)
self.mock_redis_cli.hgetall.assert_called_once_with(‘key1’)
def test_something_2(self):
self.mock_redis_cli.hgetall.return_value = {‘f21’: ‘value21’, ‘f22’: ‘value22’}
self.sut.get_hgetall_value(‘key2’)
self.mock_redis_cli.hgetall.assert_called_once_with(‘key2’)
def test_something_3(self):
self.mock_redis_cli.hgetall.return_value = {‘f1’: ‘value1’, ‘f2’: ‘value2’}
self.assertDictEqual({‘f1’: ‘value1’, ‘f2’: ‘value2’}, self.sut.get_hgetall_value(‘key1’))
if __name__ == ‘__main__’:
unittest.main()
In my real test module there are more than 200 tests, but here it is not important to show all the test methods (the same I have not showed all the methods of the class RedisCliWrapper).
How instantiate the attribute mock_redis_cli
In the previous test code the attribute mock_redis_cli is instantiated by the use the function mock.create_autospec():
self.mock_redis_cli = mock.create_autospec(redis.Redis)
If I execute the 3 tests, I obtain the following output:
> python test_redis_cli_wrapper.py
…
———————————————————————-
Ran 3 tests in 0.495s
OK
which shoes that the 3 tests are execute in 0.495 s (the execution of my real test code takes about 40 seconds).
If I instantiate the attribute mock_redis_cli by the following instruction:
self.mock_redis_cli = mock.Mock()
so without the use of the function mock.create_autospec(redis.Redis), the output of the execution of the 3 tests is the following:
> python test_redis_cli_wrapper.py
…
———————————————————————-
Ran 3 tests in 0.001s
OK
so the 3 tests are executed in only 1 ms (the execution of my real test code with the use of mock.Mock() instead of mock.create_autospec(redis.Redis) takes about 0.130 seconds).
My question
The time execution difference is enormous so in my context it is recommended to use mock.create_autospec(redis.Redis) or I can use the faster mock.Mock() instantiate instruction?
Source: Read More