Using mock.create_autospec() with the redis.Redis class increases test execution time too much

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật