vendredi 10 janvier 2020

How to test a method whose sole purpose is to calculate its sleep duration and sleep it

How am I supposed to test a method like this? (without having to wait the sleep duration)

"""Sleeps until the minute count on the digital clock is divisible by 5."""
@staticmethod
def sleepUntilMinuteDivisibleBy5():
    seconds_left = Time.calculateSecondsLeft()
    minutes_left = Time.calculateMinutesLeft()
    sleep(minutes_left * 60 + seconds_left)

Here is how I would do it if I didn't care about having to wait the duration

from unittest import TestCase, main
from Time import Time   # This is my time class
from datetime import datetime

class TestTime(TestCase):

    def test_getHowManySecondsLeft(self):
        DT = datetime.now()  # datetime object

        time = DT.time()     # Get the current time
        Time.sleepUntilMinuteDivisibleBy5()  # Sleep until the minutes is divisble by 5

        assert (DT.time().minute % 5) == 0  # Check the minutes is divisible by 5
                                            # (We have 60 seconds to do this, so 
                                            #  more than enough time)

Aucun commentaire:

Enregistrer un commentaire