UnitTest Framework – Time Test

UnitTest Framework - Time Test

UnitTest Framework Time Test is Junit, the Java unit testing framework (Pyunit is the implementation of JUnit) has a handy option of timeout. If a test takes more than the specified time, it will be marked as failed.

Python’s testing framework doesn’t contain any support for time out. However, a third-party module called timeout-decorator can do the job.

Download and install the module from −

https://pypi.python.org/packages/source/t/timeout-decorator/timeout-decorator-0.3.2.tar.gz

  • Import timeout_decorator in the code
  • Put timeout decorator before the test
  • @timeout_decorator.timeout(10)

For example −

import time
import timeout_decorator

class timeoutTest(unittest.TestCase):

   @timeout_decorator.timeout(5)
   def testtimeout(self):
      print "Start"
   for i in range(1,10):
      time.sleep(1)
      print "%d seconds have passed" % i
      
if __name__ == '__main__':
   unittest.main()

Next Topic – Click Here

This Post Has One Comment

Leave a Reply