How to stub time.sleep() in Python unit testing

Michel Keijzers

I want to make a stub to prevent time.sleep(..) to sleep to improve the unit test execution time.

What I have is:

import time as orgtime

class time(orgtime):
    '''Stub for time.'''
    _sleep_speed_factor = 1.0

    @staticmethod
    def _set_sleep_speed_factor(sleep_speed_factor):
        '''Sets sleep speed.'''
        time._sleep_speed_factor = sleep_speed_factor


    @staticmethod
    def sleep(duration):
        '''Sleeps or not.'''
        print duration * time._sleep_speed_factor
        super.sleep(duration * time._sleep_speed_factor) 

However, I get the following error on the second code line above (class definition):

TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given).

How to fix the error?

Mikko Ohtamaa

You can use mock library in your tests.

import time
from mock import patch

class MyTestCase(...):


     @patch('time.sleep', return_value=None)
     def my_test(self, patched_time_sleep):
          time.sleep(666)  # Should be instant

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to Mock/Stub or simply ignore HttpRequest when unit testing

From Dev

stub and mock a subrotine for unit testing in perl

From Dev

time.sleep() in unit test Python when using threads

From Dev

AngularJS: How do I stub a function inside a controller which I am unit-testing?

From Dev

How do you stub private functions when unit testing a revealing module

From Dev

python: testing the outputs ( ... not unit testing ?)

From Dev

Where to load stub data from when unit testing

From Dev

Mock/Stub super constructor invocation in Java for unit testing

From Dev

Unit testing for "FileNotFoundError" in python

From Dev

Python - unit testing

From Dev

How to do unit testing

From Dev

How to unit test/stub async call in redux

From Dev

how to correctly use stub for testing in rspec

From Dev

How to delete (tearDown) a singleton instance decorator in Python for unit testing?

From Dev

Python - How to do unit testing of functions deleting something?

From Dev

Python - How to do unit testing of functions deleting something?

From Dev

How to prevent code exit during Python 3.6 unit testing

From Dev

How to break time.sleep() in a python concurrent.futures

From Dev

How to pause Python's time.sleep() function?

From Dev

Is there an assert for sublists in Python unit testing?

From Dev

Change OS for unit testing in Python

From Dev

mock open for unit testing in python

From Dev

Python unit testing without django

From Dev

Testing for stdout in Python Unit Test

From Dev

Unit Testing for a Python RESTful API

From Dev

Python Unit Testing Mock incorrectly

From Dev

Singleton destructor for unit testing in Python

From Dev

mock open for unit testing in python

From Dev

Singleton destructor for unit testing in Python

Related Related

  1. 1

    How to Mock/Stub or simply ignore HttpRequest when unit testing

  2. 2

    stub and mock a subrotine for unit testing in perl

  3. 3

    time.sleep() in unit test Python when using threads

  4. 4

    AngularJS: How do I stub a function inside a controller which I am unit-testing?

  5. 5

    How do you stub private functions when unit testing a revealing module

  6. 6

    python: testing the outputs ( ... not unit testing ?)

  7. 7

    Where to load stub data from when unit testing

  8. 8

    Mock/Stub super constructor invocation in Java for unit testing

  9. 9

    Unit testing for "FileNotFoundError" in python

  10. 10

    Python - unit testing

  11. 11

    How to do unit testing

  12. 12

    How to unit test/stub async call in redux

  13. 13

    how to correctly use stub for testing in rspec

  14. 14

    How to delete (tearDown) a singleton instance decorator in Python for unit testing?

  15. 15

    Python - How to do unit testing of functions deleting something?

  16. 16

    Python - How to do unit testing of functions deleting something?

  17. 17

    How to prevent code exit during Python 3.6 unit testing

  18. 18

    How to break time.sleep() in a python concurrent.futures

  19. 19

    How to pause Python's time.sleep() function?

  20. 20

    Is there an assert for sublists in Python unit testing?

  21. 21

    Change OS for unit testing in Python

  22. 22

    mock open for unit testing in python

  23. 23

    Python unit testing without django

  24. 24

    Testing for stdout in Python Unit Test

  25. 25

    Unit Testing for a Python RESTful API

  26. 26

    Python Unit Testing Mock incorrectly

  27. 27

    Singleton destructor for unit testing in Python

  28. 28

    mock open for unit testing in python

  29. 29

    Singleton destructor for unit testing in Python

HotTag

Archive