2

With the code

import unittest
import time

class SleepingTest(unittest.TestCase):

    def test_that_gets_stuck(self):
        for a in xrange(100000000000000000):
            pass

I get this output

❯ nosetests use_nosetest.py
^C
----------------------------------------------------------------------
Ran 1 test in 4.267s

OK

As you can see, I did Ctrl+C to interrupt the program. But nose says it ran the test OK. I was rather hoping for it to say that the test fails and give me a stack trace.

Is there any way I can have a stack trace printed from where my tests get stuck?

Tarrasch
  • 10,199
  • 6
  • 41
  • 57
  • 1
    There is nothing in your test case that fails. In order to get a stack trace, some condition needs to go wrong somewhere between the time your test is started and the moment you Ctrl+C. – ILostMySpoon Sep 10 '15 at 16:03
  • 2
    @ILostMySpoon, ok. How do a python developer then debug why his/her program hangs in a unittest? – Tarrasch Sep 10 '15 at 16:30

1 Answers1

0

Try capturing your Ctrl-C signal to prompt into debugger, i.e.:

import unittest
import time

from nose.tools import set_trace

class SleepingTest(unittest.TestCase):

    def test_that_gets_stuck(self):
        try:
            for a in xrange(10000000):
                time.sleep(1)
        except KeyboardInterrupt, err:
            set_trace()
Community
  • 1
  • 1
Oleksiy
  • 6,337
  • 5
  • 41
  • 58