0

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!")
1
  • What do you want to compare? Commented Aug 12, 2014 at 22:11

1 Answer 1

2

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Hey So i would want to check the system output. So to check if the while loop is doing the countdown if that makes sense. So essentially I would be checking the timer on each iteration. @dilip
thank you very much. In short its for a system that will teach students how to program functions like these. So for example I would say "Hey create a timer variable that countsdown" thus I would check if they implemented it correctly using the test suite. @dilip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.