【问题标题】:nosetests: Getting a stack trace on Ctrl+C鼻子测试:在 Ctrl+C 上获取堆栈跟踪
【发布时间】:2015-09-10 15:47:19
【问题描述】:

有代码

import unittest
import time

class SleepingTest(unittest.TestCase):

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

我得到这个输出

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

OK

如您所见,我使用 Ctrl+C 来中断程序。但是鼻子说它运行测试正常。我更希望它说测试失败并给我一个堆栈跟踪

有什么方法可以从我的测试卡住的地方打印堆栈跟踪?

【问题讨论】:

  • 您的测试用例中没有任何内容失败。为了获得堆栈跟踪,在您的测试开始到您按 Ctrl+C 的那一刻之间,某些条件需要出错。
  • @ILostMySpoon,好的。 python 开发人员如何调试为什么他/她的程序在单元测试中挂起?

标签: python stack-trace nose python-unittest nosetests


【解决方案1】:

尝试capturing您的 Ctrl-C 信号以提示进入调试器,即:

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()

【讨论】:

    猜你喜欢
    • 2010-10-11
    • 2015-06-15
    • 2010-09-11
    • 2011-06-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多