Reputation: 407
I'm trying to figure out how I would go about creating a test suite for a while loop that outputs a basic string. I have a pretty basic countdown timer, and I just want to test it to see if it works properly. I've done unit test before, but never for loops. Below is the code I'm trying to test. I just want to be able to get the 'OK' back. Thank you in advance for all the help!
import time
timer = 11
while timer > 0:
timer = timer - 1
print(timer)
time.sleep(1)
print("Times up!")
Upvotes: 0
Views: 1610
Reputation: 2088
I did not understand what exactly you want to compare to determine whether the test has failed or passed. But if you are looking for the unit test snippet, then here you go
A very crude way to check it
import unittest
import time
expectedOutput = range(5)
class UnitTest(unittest.TestCase):
def test_while(self):
# Put your code here.
timer = 5
while timer > 0:
timer = timer - 1
self.assertTrue(timer == expectedOutput[timer])
if __name__ == "__main__":
suite = unittest.TestLoader().loadTestsFromTestCase(UnitTest)
unittest.TextTestRunner(verbosity=2).run(suite)
Upvotes: 2