python – Enhance the take a look at efficiency avoiding the usage of mock.create_autospec()

0
18
python – Enhance the take a look at efficiency avoiding the usage of mock.create_autospec()


I’ve created a category referred to as RedisCliWrapper which incorporates many strategies that decision strategies of the category redis.Redis. Beneath I present the category with solely the strategy get_hgetall_value(), which is one in all its many strategies:

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)

Because the code reveals, after I create an occasion of RedisCliWrapper I’ve to cross an occasion of the category redis.Redis.

To check the RedisCliWrapper I’ve created the next:

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.most important()

The attribute mock_redis_cli is instantiated by means of the perform mock.create_autospec():

self.mock_redis_cli = mock.create_autospec(redis.Redis)

If I execute the three assessments, I acquire the next output:

> python test_redis_cli_wrapper.py
...
----------------------------------------------------------------------
Ran 3 assessments in 0.495s

OK

which reveals that the three assessments are execute in 0.495s (the execution of my actual take a look at code takes about 40 seconds).

Nevertheless, if I instantiate the attribute mock_redis_cli by the next instruction:

self.mock_redis_cli = mock.Mock()

the output of the execution of the three assessments is the next:

> python test_redis_cli_wrapper.py
...
----------------------------------------------------------------------
Ran 3 assessments in 0.001s

OK

so the three assessments are executed in solely 1ms (the execution of my actual take a look at code with the usage of mock.Mock() as a substitute of mock.create_autospec(redis.Redis) takes about 0.130 seconds).

The time execution distinction is big so in my context it’s endorsed to make use of mock.create_autospec(redis.Redis) or can I exploit the quicker mock.Mock()?

LEAVE A REPLY

Please enter your comment!
Please enter your name here