Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I limit the maximum running time for a unit test?

Tags:

python

pytest

I am currently running some unit tests that might either take a long time before failing or run indefinitely. In a successful test run they will always complete within a certain amount of time.

Is it possible to create a pytest unit test that will fail if it does not complete within a certain amount of time?

like image 308
shuttle87 Avatar asked Oct 22 '13 20:10

shuttle87


People also ask

How long should a unit test take to run?

Still, it seems as though a 10 second short-term attention span is more or less hard-wired into the human brain. Thus, a unit test suite used for TDD should run in less than 10 seconds. If it's slower, you'll be less productive because you'll constantly lose focus.

How fast should a unit test be?

All unit tests should run in under a second (that is all unit tests combined should run in 1 second).

Does Pytest have a timeout?

For each test item the pytest-timeout plugin starts a timer thread which will terminate the whole process after the specified timeout. When a test item finishes this timer thread is cancelled and the test run continues.

How do you run a test unit test?

To run all the tests in a default group, choose the Run icon and then choose the group on the menu. Select the individual tests that you want to run, open the right-click menu for a selected test and then choose Run Selected Tests (or press Ctrl + R, T).


1 Answers

you can install the pytest-timeout plugin and then mark your test functions with a timeout in seconds.

@pytest.mark.timeout(300) def test_foo():    pass 

Look at the plugin download and usage instructions at https://pypi.python.org/pypi/pytest-timeout

like image 79
Mrinal Shukla Avatar answered Sep 29 '22 06:09

Mrinal Shukla