12

I'm setting up my nosetests environment but can't seem to get the timeout to work properly. I would like to have an x second (say 2) timeout on each test discovered by nose.

I tried the following:

nosetests --processes=-1 --process-timeout=2

This works just fine but I noticed the following:

  • Parallel testing takes longer for a few simple testcases
  • Nose does not report back when a test has timed out (and thus failed)

Does anyone know how I can get such a timeout to work? I would prefer it to work without parallel testing but this would not be an issue as long as I get the feedback that a test has timed out.

JustMe
  • 237
  • 4
  • 7

1 Answers1

11

I do not know if this will make your life easier, but there is a similar functionality in nose.tools that will fail on timeout, and you do not have to have parallel testing for it:

from nose.tools import timed

@timed(2)
def test_a():
    sleep(3)

You can probably auto decorate all your tests in a module using a script/plugin, if manually adding an attribute is an issue, but I personally prefer clarity over magic.

By looking through the Lib/site-packages/nose/plugins/multiprocess.py source it looks like process-timeout option that you are using is somewhat specific to managing "hanging" subprocesses that may be preventing test from completion.

Oleksiy
  • 6,337
  • 5
  • 41
  • 58
  • 3
    Works as expected, thanks! Although I would prefer a command line option to set a timeout for all testscripts I will continue with this approach. This will actually allow me to set timeouts for specific functions which are expected to take longer than others – JustMe May 14 '14 at 07:54
  • 9
    I tried this and was unhappy. Here is what I put in my code : import nose.tools.timed # Warning, this does not do as advertized, don't use, this line left as warning # nose.tools.timed does not limit the time a test has to run, it runs the test, and if it took too long # then it raises TimeExpired(), this provides neither a bound on the run time, nor a way to extend the # running time of a test that takes longer than the nose-wide per-test limit. Why would anyone ever use this? – Jim Jul 31 '15 at 21:50
  • @Jim, I found `nose.tools.timed` very useful to check that S3 network retry strategies take as long as it said it would. The test is supposed to take e.g. 0.8 seconds and then I'll fail it if it takes more than 1 second. In general it is hard to limit the time a test has to run, since you have to stop processes forcefully via OS mechanisms. The `nose.tools.timed` function, on the other hand, is simple and useful for very specific use cases. It reminds me of the disclaimer of the `pytest-timeout` package (which does what you want): "This is not the timeout you are looking for!" – llude Nov 28 '22 at 11:06