Вы можете использовать unittest.mock.patch , чтобы смоделировать time.sleep
метод. Затем он будет выполнен немедленно.
Например,
Time.py
:
from time import sleep
class Time:
@staticmethod
def sleepUntilMinuteDivisibleBy5():
seconds_left = Time.calculateSecondsLeft()
minutes_left = Time.calculateMinutesLeft()
sleep(minutes_left * 60 + seconds_left)
@staticmethod
def calculateMinutesLeft():
return 1
@staticmethod
def calculateSecondsLeft():
return 40
test_Time.py
:
import unittest
from unittest.mock import patch
from Time import Time
from datetime import datetime
class TestTime(unittest.TestCase):
@patch('Time.sleep', return_value=None)
def test_sleepUntilMinuteDivisibleBy5(self, mock_sleep):
Time.sleepUntilMinuteDivisibleBy5()
mock_sleep.assert_called_with(100)
if __name__ == '__main__':
unittest.main()
Результаты модульного теста с 100 % покрытия:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Name Stmts Miss Cover Missing
-----------------------------------------------------------------------
src/stackoverflow/59680752/Time.py 10 0 100%
src/stackoverflow/59680752/test_Time.py 10 0 100%
-----------------------------------------------------------------------
TOTAL 20 0 100%