The production code
I’m working with the database Redis and I’m interfacing to it by the redis-py Python client.
For a reason not important for this question I have created a production class called RedisCliWrapper where I have written many methods that call methods of the class redis.Redis
. Substantially this is a wrapper of the class redis.Redis
.
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)
The code shows that before to create an instance of RedisCliWrapper
I have to create an instance of the class redis.Redis
and pass it to the __init__()
method of RedisCliWrapper:
from redis import Redis
redis_client_instance = Redis(host=REDIS_HOST_ADDRESS, port=6379, encoding="utf-8", decode_responses=True)
redis_cli_wrapper = RedisCliWrapper(redis_client_instance)
The test code
I have also written test code for the class RedisCliWrapper
in 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() # <---- THIS COMMENTED INSTRUCTION IS IMPORTANT FOR THE QUESTION
self.sut = RedisCliWrapper(self.mock_redis_cli)
def test_hgetall_1(self):
self.mock_redis_cli.hgetall.return_value = {'field1': 'value1', 'field2': 'value2'}
self.sut.get_hgetall_value('key1')
self.mock_redis_cli.hgetall.assert_called_once_with('key1')
def test_hgetall_2(self):
self.mock_redis_cli.hgetall.return_value = {'field21': 'value21', 'field22': 'value22'}
self.sut.get_hgetall_value('key2')
self.mock_redis_cli.hgetall.assert_called_once_with('key2')
def test_hgetall_3(self):
self.mock_redis_cli.hgetall.return_value = {'field1': 'value1', 'field2': 'value2'}
self.assertDictEqual({'field1': 'value1', 'field2': 'value2'}, self.sut.get_hgetall_value('key1'))
if __name__ == '__main__':
unittest.main()
In my real test_redis_cli_wrapper.py
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
).
The attribute mock_redis_cli
creation decides the performance of the tests
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 tests, I obtain the following output:
> python test_redis_cli_wrapper.py
...
----------------------------------------------------------------------
Ran 3 tests in 0.539s
OK
which shoes that the execution of only 3 tests takes 0.593 s (the execution of my real test code takes about 40 seconds).
Instead if I instantiate the attribute mock_redis_cli
by the (commented) 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
If I have well understand, create_autospec allows to create test code more sure and in particular there are some controls to check if it is calling a method of the Mock object (self.mock_redis_cli
in my example) which is not part of the Class interface (Class redis.Redis
in my code) is raised an AttributeError
exception.
My problem is that my tests are taking too long using mock.create_autospec(redis.Redis)
so I’m tempted to use the faster test code which uses mock.Mock()
.
Is this an acceptable compromise?