Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何获取unittest中引发的异常_Python_Django_Unit Testing - Fatal编程技术网

Python 如何获取unittest中引发的异常

Python 如何获取unittest中引发的异常,python,django,unit-testing,Python,Django,Unit Testing,我有我自己的异常,我想测试ex中的其他字段,然后测试消息。 读了这篇文章,我尝试了使用上下文的想法。我编写了这个通用函数 def test_runtime_error(test, exception_type, message, display_parameter, path, callable_obj, *args): pdb.set_trace() with test.assertRaises(exception_type) as cx: callable_o

我有我自己的异常,我想测试ex中的其他字段,然后测试消息。 读了这篇文章,我尝试了使用上下文的想法。我编写了这个通用函数

def test_runtime_error(test, exception_type, message, display_parameter, path, callable_obj, *args):
    pdb.set_trace()
    with test.assertRaises(exception_type) as cx:
        callable_obj(*args)
    ex = cx.exception

    test.assertEqual(ex.message,message)
    test.assertEqual(ex.display_parameter,display_parameter)
    test.assertEqual(ex.path,path)
路径
显示参数
是我自己的特定字段。这里的轮子不是我发明的,我大部分都是从汽车里拿出来的

我就是这么用的

class ExceptionsTest(unittest.TestCase):
    def test_something(self):
         data = {"name" : "A"}
         obj = MyModel.objects.get(pk=1)
         test_runtime_error(self,CustomException, 'message', 'A', [], obj.create, data)

参数被正确地传递到可调用的_obj中。该函数引发预期的异常。但在执行可调用的_obj之后,函数立即中断,并且不会获取异常。顺便说一句,当我在测试中运行相同的代码时,它本身运行得很好


这里怎么了

这里的问题似乎是这一行:

pdb.set_trace()
如果您将其保留在中,但没有导入pdb,则以下代码将失败:

E
======================================================================
ERROR: testRaises (__main__.ExceptionTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "./except.py", line 22, in testRaises
    self.verifyComplexException(MyException, 'asdf', RaiseException, 'asdf')
  File "./except.py", line 14, in verifyComplexException
    pdb.set_trace()
NameError: global name 'pdb' is not defined

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)
与你的描述相符。如果添加了
import pdb
行,它将进入调试器,这是一种完全不同的行为,不能将退出状态与
E
或退出状态与
F
混淆,因此不能这样

下面是一个基于此思想的完整示例,它按预期工作(根据Apache 2.0获得许可;请参阅):


相反,我会创建一个基本的
TestCase
类,并在其上引入
test\u runtime\u error
作为一个方法。“函数中断,而异常没有被提取”——然后发生了什么?@shx2测试中断-异常
E
-不是测试失败
F
@alecxe-这会有什么不同吗?(另一个则更整洁…?)你的测试失败的确切例外是什么?请为问题添加完整的输出。如果删除
pdb.set\u trace()
行,会发生什么?
import unittest

class MyException(Exception):
    def __init__(self, message):
        self.message = message

def RaiseException(message):
    raise MyException(message)

class ExceptionTest(unittest.TestCase):
    def verifyComplexException(self, exception_class, message, callable, *args):
        with self.assertRaises(exception_class) as cm:
            callable(*args)

        exception = cm.exception
        self.assertEqual(exception.message, message)

    def testRaises(self):
        self.verifyComplexException(MyException, 'asdf', RaiseException, 'asdf')


if __name__ == '__main__':
    unittest.main()