How to specify test timeout for python unittest?

As far as I know unittest does not contain any support for tests timeout.

You can try timeout-decorator library from PyPI. Apply the decorator on individual tests to make them terminate if they take too long:

import timeout_decorator

class TestCaseWithTimeouts(unittest.TestCase):

    # ... whatever ...

    @timeout_decorator.timeout(LOCAL_TIMEOUT)
    def test_that_can_take_too_long(self):
        sleep(float('inf'))

    # ... whatever else ...

To create a global timeout, you can replace call

unittest.main()

with

timeout_decorator.timeout(GLOBAL_TIMEOUT)(unittest.main)()

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)