Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 14, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 14, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 14, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 14, 2025

      I test a lot of AI coding tools, and this stunning new OpenAI release just saved me days of work

      May 14, 2025

      How to use your Android phone as a webcam when your laptop’s default won’t cut it

      May 14, 2025

      The 5 most customizable Linux desktop environments – when you want it your way

      May 14, 2025

      Gen AI use at work saps our motivation even as it boosts productivity, new research shows

      May 14, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025
      Recent

      Strategic Cloud Partner: Key to Business Success, Not Just Tech

      May 14, 2025

      Perficient’s “What If? So What?” Podcast Wins Gold at the 2025 Hermes Creative Awards

      May 14, 2025

      PIM for Azure Resources

      May 14, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025
      Recent

      Windows 11 24H2’s Settings now bundles FAQs section to tell you more about your system

      May 14, 2025

      You can now share an app/browser window with Copilot Vision to help you with different tasks

      May 14, 2025

      Microsoft will gradually retire SharePoint Alerts over the next two years

      May 14, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Increase the test performance avoiding the use of mock.create_autospec()

    Increase the test performance avoiding the use of mock.create_autospec()

    July 10, 2024

    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

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleGetting “org.openqa.selenium.UnsupportedCommandException: This API is not supported anymore ” This error while running my test case
    Next Article Edge Case Testing: A Comprehensive Guide

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 15, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4695 – PHPGurukul Cyber Cafe Management System SQL Injection

    May 15, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    GNU/Linux supera il 5% di diffusione: analisi dei dati di PornHub e StatCounter

    Linux

    UX in Universal Design Series: The Role of Adaptive and Assistive Technologies in Health Systems – 7

    Development

    Artifacts: Jump Lists

    Operating Systems

    Create a 360-degree master data management patient view solution using Amazon Neptune and generative AI

    Databases

    Highlights

    5 Tips for Managing a Dispersed Project Team (Free Download)

    June 30, 2024

    Even though no technology can take the place of talking face to face, the following…

    Get a free gift card when you buy a Google Pixel 9 phone at Best Buy or Amazon

    August 13, 2024

    The Different (and Modern) Ways to Toggle Content

    December 1, 2024

    Application Continuity for Oracle workloads with Amazon RDS Custom for Oracle

    June 20, 2024
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.