
- Unit Testing - Home
- Unit Testing - Overview
- Unit Testing - Test Framework
- Unit Testing - API
- Unit Testing - Using Assertion
- Unit Testing - Test Discovery
- Unit Testing - Skip Test
- Unit Testing - Exceptions Test
- Unit Testing - Time Test
- Unit Testing - Unittest2
- Unit Testing - Signal Handling
- Unit Testing - Doctest
- Unit Testing - Doctest API
- Unit Testing - Py.test Module
- Nose Testing Framework
- Unit Testing - Tools for Nose Testing
Unit Testing - Time Test
Junit, the Java unit testing framework (Pyunit is implementation of JUnit) has a handy option of timeout. If a test takes more than specified time, it will be marked as failed.
Python's testing framework doesn't contain any support for time out. However, a third part 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)
If a test method below this line takes more than the timeout mentioned (10 mins) here, a TimeOutError will be raised. 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()
Advertisements