I'm using python
framework unittest
. Is it possible to specify by framework's abilities a timeout for test? If no, is it possible to specify gracefully a timeout
for all tests and for some separated tests a private value for each one?
I want to define a global timeout
for all tests (they will use it by default) and a timeout for some test that can take a long time.
The code in pytest is simple, compact, and efficient. For unittest, we will have to import modules, create a class and define the testing functions within that class. But for pytest, we only have to define the testing function. Pytest is also fast and efficient.
A new Python-based project called Python Test Runner ( ptr ), that allows developers to run Python unit test suites. The main difference between ptr and existing test runners is that ptr crawls a repository to find Python projects with unit tests defined in their setup files.
pytest supports running Python unittest -based tests out of the box. It's meant for leveraging existing unittest -based test suites to use pytest as a test runner and also allow to incrementally adapt the test suite to take full advantage of pytest's features.
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)()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With